This just continues the work of #9804
Modifies a lot of &vector[]'s to vector.data()'s across all the files including tests, just the stuff that 9804 missed
This just continues the work of #9804
Modifies a lot of &vector[]'s to vector.data()'s across all the files including tests, just the stuff that 9804 missed
#9804 has just been merged
443 | @@ -444,7 +444,7 @@ static inline JSONRPCRequest transformNamedArguments(const JSONRPCRequest& in, c 444 | const std::vector<UniValue>& values = in.params.getValues(); 445 | std::unordered_map<std::string, const UniValue*> argsIn; 446 | for (size_t i=0; i<keys.size(); ++i) { 447 | - argsIn[keys[i]] = &values[i]; 448 | + argsIn[keys[i]] = values.data() + i;
Is this really clearer?
153 | @@ -154,8 +154,8 @@ BOOST_AUTO_TEST_CASE(bloom_match) 154 | COutPoint prevOutPoint(uint256S("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0); 155 | { 156 | std::vector<unsigned char> data(32 + sizeof(unsigned int)); 157 | - memcpy(&data[0], prevOutPoint.hash.begin(), 32); 158 | - memcpy(&data[32], &prevOutPoint.n, sizeof(unsigned int)); 159 | + memcpy(data.data(), prevOutPoint.hash.begin(), 32); 160 | + memcpy(data.data()+32, &prevOutPoint.n, sizeof(unsigned int));
same here
If data is empty, I'd say copying to data()+32 is U/B no matter what.
19 | @@ -20,13 +20,13 @@ BOOST_AUTO_TEST_CASE(skiplist_test) 20 | 21 | for (int i=0; i<SKIPLIST_LENGTH; i++) { 22 | vIndex[i].nHeight = i; 23 | - vIndex[i].pprev = (i == 0) ? NULL : &vIndex[i - 1]; 24 | + vIndex[i].pprev = (i == 0) ? NULL : (vIndex.data() + i - 1);
same here
26 | }
27 |
28 | for (int i=0; i<SKIPLIST_LENGTH; i++) {
29 | if (i > 0) {
30 | - BOOST_CHECK(vIndex[i].pskip == &vIndex[vIndex[i].pskip->nHeight]);
31 | + BOOST_CHECK(vIndex[i].pskip == vIndex.data() + vIndex[i].pskip->nHeight);
here you aren't even consistent
what lacks consistency?
One time vIndex is accessed by vIndex.data() + n and another with vIndex[m]
That's because one is accessing the element not a pointer...
Isn't it still preferable to access the memory location of an element this way?
@MeshCollider the advantage of .data() is that it is defined to return nullptr if .empty(), compared to &v[0] potentially de-referencing unitialized memory.
In the above, we assume vIndex is not empty, as you are de-referencing vIndex[i], therefore, it is probably not as necessary.
As far as the pattern goes, #9804 introduced .data() specifically where &v[0] was used to target the front of an array, which may have been uninitialized.
That is not the case here.
36 | @@ -37,9 +37,9 @@ BOOST_AUTO_TEST_CASE(skiplist_test) 37 | int from = InsecureRandRange(SKIPLIST_LENGTH - 1); 38 | int to = InsecureRandRange(from + 1); 39 | 40 | - BOOST_CHECK(vIndex[SKIPLIST_LENGTH - 1].GetAncestor(from) == &vIndex[from]); 41 | - BOOST_CHECK(vIndex[from].GetAncestor(to) == &vIndex[to]); 42 | - BOOST_CHECK(vIndex[from].GetAncestor(0) == &vIndex[0]); 43 | + BOOST_CHECK(vIndex[SKIPLIST_LENGTH - 1].GetAncestor(from) == vIndex.data() + from);
well, you get the idea, if the index was other than 0, it seems to me that the change makes things worse.
Fast review ACK modulo nits.
Rebased
Rebased
42 | @@ -43,7 +43,7 @@ static SendCoinsRecipient handleRequest(PaymentServer* server, std::vector<unsig 43 | // Write data to a temp file: 44 | QTemporaryFile f; 45 | f.open(); 46 | - f.write((const char*)&data[0], data.size()); 47 | + f.write((const char*) data.data(), data.size());
nit: Please remove space after the case here
@MeshCollider You can pipe the diff through clang-format to prevent such nits in the future. See dev notes.
Fixed, thanks :)
utACK [s]db0e991fb7975c071818b058376644be08ced343[/s] https://github.com/bitcoin/bitcoin/pull/10793/commits/7d5d19903168bb9452e386ace62b74875c04dae2
The commit message is weird now :)
Argh fixed, thanks :)