Skip to content

Instantly share code, notes, and snippets.

@idoleat
Created May 29, 2023 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idoleat/e3859d353e489f2b8298ae444192d976 to your computer and use it in GitHub Desktop.
Save idoleat/e3859d353e489f2b8298ae444192d976 to your computer and use it in GitHub Desktop.
#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