Here this code likely does the right thing but for very wrong reasons :
vector<uint8_t> col(24); // And a new raw SPI data value
int cntTotal = 0; // Counter initializations
int cntCol = 0;
...
while (cntCol < sizeof(col) / sizeof(col.at(0))) {
...
}
this gives:
while (cntCol < (24 / 1)). But the reason sizeof(col) is 24 is not because it was initialized with 24 bytes : most likely you'll be able to confirm that
also prints 24 24.. that's because sizeof(x) returns the "C" size of the class, not the logical size / number of elements in the container... and it happens that std::vector is implemented with three pointers:
and on 64 bit systems, sizeof(T) == 8 bytes, thus 8 3 == 24 and sizeof(vector) == 24 for any std::vector instance no matter how many elements there are.
except...
on 32 bit systems it would actually be 12
with safety features such as address sanitizer or CHERI it may be bigger
Thankyou for pointing this out!
I noticed this too a few days ago because I am in the middle of a rewrite for this library but I moved it to GitLab (I forgot to mention this in the README)
And I will include this fix in the rewrite
https://github.com/hannescam/NeoSPI/blob/b7ef5877db186c2ddd5c08d6d3ba1897c60fd8f6/src/NeoSPI.cpp#L149
Here this code likely does the right thing but for very wrong reasons :
this gives:
while (cntCol < (24 / 1))
. But the reason sizeof(col) is 24 is not because it was initialized with 24 bytes : most likely you'll be able to confirm thatalso prints 24 24.. that's because sizeof(x) returns the "C" size of the class, not the logical size / number of elements in the container... and it happens that std::vector is implemented with three pointers:
and on 64 bit systems, sizeof(T) == 8 bytes, thus 8 3 == 24 and sizeof(vector) == 24 for any std::vector instance no matter how many elements there are.
except...
thus I'd really recommend to just spell out
or even directly
as it's never supposed to not be 24 from the code