3 ms·
The issue with 1 is that it only works until you pass an array into a function by pointer, then the macro no longer works. In my experience it's most likely th
by ChrisRR 3y ago
The issue with 1 is that it only works until you pass an array into a function by pointer, then the macro no longer works.
In my experience it's most likely that a function will write past the bounds of a buffer that's been passed as an argument. In that case, make sure the size of array is always included as an argument as you said in 4.
- ollien 3y ago> The issue with 1 is that it only works until you pass an array into a function by pointer, then the macro no longer works. GCC even has a warning for this. Even worse, even if you specify the argument to be "of the type" array, it will actually still decay to a pointer. Basically, this macro will only work if you use it in the same function the array is defined. https://godbolt.org/z/vr4za73qq https://godbolt.org/z/vr4za73qq
- bheadmaster 3y agoOne exception is if you explicitly define argument as array of fixed length. Downside being, obviously, that it will only work with arrays of that particular length.
- uecker 3y agoYou need to pass a pointer to an array: https://godbolt.org/z/jYzY79ac4 https://godbolt.org/z/jYzY79ac4 When passing an array it decays into a pointer and the size is lost. We can also change sizeof to recover it, but there was a proposal for a _Lengthof operator which could work here.
- mzs 3y agosee item 4