Created
October 8, 2020 05:23
-
-
Save demotomohiro/02faa1250fa105d3e41a22ba865c1179 to your computer and use it in GitHub Desktop.
Print offsets of object field in Nim
This file contains 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
#include <stdio.h> | |
#include <stddef.h> | |
#define PRINTOFFSET(typ, field) printf("%s(%ld): %ld\n", #field, sizeof(((typ*)0)->field), offsetof(typ, field)) | |
typedef struct { | |
char a; | |
int b; | |
short c; | |
double d; | |
} someStrct; | |
int main() { | |
PRINTOFFSET(someStrct, a); | |
PRINTOFFSET(someStrct, b); | |
PRINTOFFSET(someStrct, c); | |
PRINTOFFSET(someStrct, d); | |
return 0; | |
} |
This file contains 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
proc printSizeAndOffset(x: typedesc[object | tuple]) = | |
echo x, ':' | |
for name, field in cast[ptr x](0)[].fieldPairs: | |
echo " ", name, "(", sizeof(field), "): ", cast[uint](addr field) | |
(a:10, b:"aa").typeof.printSizeAndOffset | |
type | |
someObj = object | |
a: char | |
b: int | |
c: int16 | |
d: float | |
someObj.printSizeAndOffset |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment