I came across the macro below
#define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
I kind of not able to digest this because in c++, when I try to deference a null pointer, I expect an unexpected behaviour… but how come it can have an address? what does address of null mean?
For the purpose of the macro:
It assumes that there is an object of type
TYPEat address 0 and returns the address of the member which is effectively the offset of the member in the structure.This answer explains why this is undefined behaviour. I think that this is the most important quote:
which is the case here. Although others think that this is valid. It is important to note that this will produce the correct result on many compilers though.