Skip to content

Instantly share code, notes, and snippets.

@mastbaum
Created June 17, 2011 18:32
Show Gist options
  • Save mastbaum/1031989 to your computer and use it in GitHub Desktop.
Save mastbaum/1031989 to your computer and use it in GitHub Desktop.
malloc'ing a struct of structs, pointer edition
#include<stdio.h>
#include<malloc.h>
typedef struct
{
unsigned long a;
} Bar;
typedef struct
{
Bar* b[1000000];
} Foo;
int main()
{
printf("sizeof(Foo) = %d\n", sizeof(Foo));
printf("sizeof(Bar) = %d\n", sizeof(Bar));
Foo* f = (Foo*) malloc(sizeof(Foo));
unsigned long i;
for(i=0; i<1000000; i++) {
Bar* b = (Bar*) malloc(sizeof(Bar));
b->a = 65535;
f->b[i] = b;
}
printf("%i\n", f->b[50]->a);
printf("sleep\n");
unsigned long j;
for(j=0;j<2400000000;j++) continue;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment