I have seen this complex declaration of sizeof operator somewhere in book which is teasing me:
#define SIZEOF(int) (int)&((int *)0)[1]
Can any one explain this declaration, whats going on here…?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here it is with the
[1]part ‘expanded’:Now do you see how it works?
The 0 will increase by the
sizeofintdue to the properties of pointer arithmetic and the finalintcast is getting you the value of the resulting address which is the ‘size’.It’s totally undefined behavior though (the arithmetic on null and possibly the dereference of invalid pointer), fairly pointless.
The update with the macro parameter doesn’t make much sense. The final cast should probably be
std::uintptr_t.