Last active
December 11, 2015 02:38
-
-
Save jonpryor/4531907 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cat ofs.cs | |
using System; | |
using System.Runtime.InteropServices; | |
[StructLayout (LayoutKind.Explicit)] | |
struct Embedded { | |
[FieldOffset (0)] IntPtr b; | |
[FieldOffset (0)] int c; | |
} | |
struct X { | |
int A; | |
Embedded B; | |
} | |
class Test { | |
public static void Main () | |
{ | |
Console.WriteLine ("offsetof(struct X, b)={0}", Marshal.OffsetOf (typeof (X), "B")); | |
} | |
} | |
$ csc /t:exe /platform:x86 /out:ofs-32.exe ofs.cs | |
$ csc /t:exe /platform:x64 /out:ofs-64.exe ofs.cs | |
$ ofs-32.exe | |
offsetof(struct X, b)=4 | |
$ ofs-32.exe | |
offsetof(struct X, b)=8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cat ofs.c | |
/* offsets */ | |
#include <stdio.h> | |
#include <stddef.h> | |
struct X { | |
int a; | |
union { | |
void *b; | |
int c; | |
}; | |
}; | |
int main () | |
{ | |
printf ("offset(X::b)=%i\n", (int) offsetof (struct X, b)); | |
return 0; | |
} | |
$ gcc -m32 ofs.c -o ofs-32 | |
$ gcc -m64 ofs.c -o ofs-64 | |
$ ./ofs-32 | |
offset(X::b)=4 | |
$ ./ofs-64 | |
offset(X::b)=8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment