Skip to content

Instantly share code, notes, and snippets.

@jonpryor
Last active December 11, 2015 02:38
Show Gist options
  • Save jonpryor/4531907 to your computer and use it in GitHub Desktop.
Save jonpryor/4531907 to your computer and use it in GitHub Desktop.
$ 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
$ 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