Skip to content

Instantly share code, notes, and snippets.

@mastbaum
Created June 17, 2011 18:33
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 mastbaum/1031993 to your computer and use it in GitHub Desktop.
Save mastbaum/1031993 to your computer and use it in GitHub Desktop.
malloc'ing a struct of structs, instance 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].a = 65535;
}
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