Last active
March 10, 2025 21:38
-
-
Save x-zvf/e1909ebdcf692036959a7e81ca0d9611 to your computer and use it in GitHub Desktop.
Print the in-memory layout of a C struct | C Macros are awesome
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
#include <stdint.h> | |
#include<stdio.h> | |
#define FIELDS \ | |
X(int32_t, a) \ | |
X(char, b) \ | |
X(char *,lonnngname) \ | |
X(short, d) | |
struct mystruct { | |
#define X(type, name) type name; | |
FIELDS | |
#undef X | |
} S; | |
#define PRINT_PADDING(offset, padding) \ | |
if(padding) \ | |
printf(" /* %02lu */ /* padding */ // %lu\n", offset, padding); | |
#define PRINT_VAR(offset, type, name) \ | |
printf(" /* %02lu */ %-10s %10s; // %lu\n", offset, #type, #name, sizeof(S.name)); | |
int main(void) { | |
intptr_t last_offset = 0; | |
intptr_t last_size = 0; | |
intptr_t offset, padding, padding_start; | |
printf("// offset type name size\n"); | |
printf(" struct mystruct { // %lu\n\n", sizeof(S)); | |
#define X(type, name)\ | |
offset = (intptr_t)(&S.name) - (intptr_t)(&S); \ | |
padding_start = last_offset + last_size; \ | |
padding = offset - padding_start; \ | |
last_offset = (intptr_t)(&S.name) - (intptr_t)(&S); \ | |
last_size = sizeof(S.name);\ | |
\ | |
PRINT_PADDING(padding_start, padding) \ | |
PRINT_VAR(offset, type, name); | |
FIELDS | |
#undef X | |
PRINT_PADDING(last_offset + last_size, sizeof(S) - last_offset - last_size); | |
printf("};\n"); | |
} | |
Author
x-zvf
commented
Mar 10, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment