2009/9/23 Chris Robinson chris.kcat@gmail.com:
On Tuesday 22 September 2009 12:32:35 am Mike Kaplinskiy wrote:
It actually does not dereference anything.
Does the C standard specify that taking the address of a struct member being dereferenced doesn't actually cause a dereference, instead just offsetting? Doing foo-> is identical to (*foo)., so dmW->dmFormName is the same as &(*dmW).dmFormName, which does technically cause a dereference, followed by taking the address of the field.
However, since GCC will remove deadcode and it's simple to see the dereference isn't needed, it just optimizes it away. I wouldn't even be surprised if this behavior is guaranteed by GCC with no optimizations enabled.. but I'm not so sure that it's guaranteed by the C standard. Is it?
Worth trying my sample code on non-GCC compilers then: #include <stdio.h> #include <stdlib.h>
struct foo { int baz[1]; };
int main() { struct foo *bar=NULL; printf("%p\n",(void*)(NULL)); printf("%p\n",(void*)(bar->baz)); printf("%p\n",(void*)((*bar).baz)); printf("%p\n",(void*)(&(*bar).baz[5])); printf("%d\n",(sizeof(int[5]))); return 0; }
Expected output: (nil) (nil) (nil) 0x14 20
Is there a reason why sizeof() magic can't be used instead of FIELD_OFFSET? :)