3 ms·
I would add one more: the address you are dereferencing could be non-zero, it could be an offset from 0 because the code is accessing a field in a structure or
by alain94040 2y ago
I would add one more: the address you are dereferencing could be non-zero, it could be an offset from 0 because the code is accessing a field in a structure or method in a class. That offset can be quite large, so if you see an error accessing address 0x420, it's probably because you do have a null pointer and are trying to access a field. As a bonus, the offending offset may give you a hint as to which field and therefore where in your code the bad dereferencing is happening.
- catlifeonmars 2y agoNow this is a really interesting one. I’m assuming this trivially applies to index array access as well?
- kevincox 2y agoI think this technically wouldn't be a null pointer anymore. As array indexing `p[n]` is defined as `*(p + n)` so first you create a new pointer by doing math on a null pointer (which is UB in C) then dereferencing this new pointer (which doesn't even really exist because you have already committed UB).
- tialaramex 2y agoIn C, and this article seems to be almost exclusively about C, a[b] is basically sugar for (*((a) + (b))) C does actually have arrays (don't let people tell you it doesn't) but they decay to pointers at ABI fringes and the index operation is, as we just saw, merely a pointer addition, it's not anything more sophisticated - so the arrays count for very little in practice.
- juped 2y agoNot just basically sugar, the classic parlor trick is doing 3[arr] or whatever
- catlifeonmars 2y agoParty trick? That’s just good coding style. I even have that enabled as a lint rule /s
- nyanpasu64 2y agoOne interesting failure mode is if (like the Linux kernel) a function returns a union of a pointer or a negative errno value, dereferencing a negative errno gives an offset (below or above zero) different from the field being accessed.