Skip to content

Instantly share code, notes, and snippets.

@nelhage
Created May 19, 2011 19:08
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 nelhage/981483 to your computer and use it in GitHub Desktop.
Save nelhage/981483 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
struct hentry {
int foo;
char word[1];
};
int main(void) {
struct hentry h, *h1 = &h, *h2 = (struct hentry*)malloc(sizeof(struct hentry) - 1 + 10);
printf("stack (0): %ld\n", __builtin_object_size(h1, 0));
printf("stack (1): %ld\n", __builtin_object_size(h1, 1));
printf("stack (2): %ld\n", __builtin_object_size(h1, 2));
printf("stack (3): %ld\n", __builtin_object_size(h1, 3));
printf("heap (0): %ld\n", __builtin_object_size(h2, 0));
printf("heap (1): %ld\n", __builtin_object_size(h2, 1));
printf("heap (2): %ld\n", __builtin_object_size(h2, 2));
printf("heap (3): %ld\n", __builtin_object_size(h2, 3));
return 0;
}
/*
[nelhage@psychotique:/tmp]$ gcc -O3 -o size size.c
[nelhage@psychotique:/tmp]$ ./size
stack (0): 8
stack (1): 8
stack (2): 8
stack (3): 8
heap (0): 17
heap (1): 17
heap (2): 17
heap (3): 17
[nelhage@psychotique:/tmp]$ g++ -O3 -o size size.c
[nelhage@psychotique:/tmp]$ ./size
stack (0): 8
stack (1): 8
stack (2): 8
stack (3): 8
heap (0): 17
heap (1): 17
heap (2): 17
heap (3): 17
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment