Macro explaination
-
Can anyone help me explaining what the below macro is supposed to do. #define OFF(type, field) ((LONG)(LONG_PTR)&(((type *)0)->field)) d2hes
It gives the offset of a structure member from the beginning of the structure, in bytes.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
It gives the offset of a structure member from the beginning of the structure, in bytes.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
#define OFF(type, field) ((LONG)(LONG_PTR)&(((type *)0)->field))
Take the following structure:
struct ThisStruct
{
char field1; // at offset 0
short field2; // at offset 1
int field3; // at offset 3
int field4; // at offset 7
};If we call the macro as such:
OFF(ThisStruct, field3)
Firstly, the macro creates a pointer to a ThisStruct structure at address 0:((ThisStruct *)0)
Next, it refers to a particular field inside that structure:((ThisStruct *)0)->field3
Next, it takes the address of that field:&(((ThisStruct *)0)->field3)
Sincefield3
is 3 bytes from the beginning of the structure, and the structure is at address 0, the pointer will hold the value 3. Lastly, the macro converts the pointer to aLONG_PTR
and finally to aLONG
, to give an integer result - the offset of the field from the beginning of the structure. Hope this helps,Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"