Created
May 29, 2023 23:29
-
-
Save idoleat/e3859d353e489f2b8298ae444192d976 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
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#define my_sizeof(type) \ | |
({ \ | |
__typeof__(type) _a[2]; \ | |
(size_t) & _a[1] - (size_t)&_a[0]; \ | |
}) | |
/* The lost art of structure packing: | |
* http://www.catb.org/esr/structure-packing/ | |
* | |
* So how did the declairation order decided in those huge structs in Linux kernel? | |
*/ | |
struct st { | |
int a; | |
int b; | |
char ss; | |
int *kk; | |
}; | |
int main() { | |
int c[7]; | |
struct st ttt; | |
printf("my_sizeof: %lu\n", my_sizeof(ttt)); | |
printf("sizeof: %lu\n", sizeof(ttt)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment