Created
September 12, 2011 11:01
-
-
Save flavius/1211017 to your computer and use it in GitHub Desktop.
This file contains 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 <stdio.h> | |
#include <stdlib.h> | |
#define NUMS 10 | |
typedef struct _foo { | |
char *t; | |
int i; | |
} foo; | |
typedef struct _foo_container { | |
foo ***foos; | |
} container; | |
foo** init_foos(void) { | |
foo** f, *f2; | |
int i; | |
f = malloc(sizeof(foo*)*NUMS); | |
for(i=0; i < NUMS; i++) { | |
f2 = malloc(sizeof(foo)); | |
printf("allocated %p ", f2); | |
f[i] = f2; | |
f[i]->i = i*11+1; | |
printf("with value %d\n", i*11+1); | |
} | |
return f; | |
} | |
void dump_container(container *c) { | |
int i; | |
for(i=0; i < NUMS; i++) { | |
//printf("foo[%d]=%d at %p\n", i, (*c->foos[i])->i, *(c->foos[i])); | |
printf("foo[%d]=%d at %p\n", i, (*c->foos[i])->i, *c->foos[i]); | |
} | |
} | |
int main(void) { | |
container c; | |
foo **foos; | |
foos = init_foos(); | |
c.foos = &foos; | |
int i; | |
for(i=0; i < NUMS; i++) { | |
printf("%d %d at %p\n", i, foos[i]->i, foos[i]); | |
} | |
printf("------------------\n"); | |
dump_container(&c); | |
/* | |
for(i=0; i < NUMS; i++) { | |
//printf("%d %d\n", i, (**c.foos[i]).i); | |
printf("%d %d at %p\n", i, (*(*c.foos)[i]).i); | |
free(foos[i]); | |
} | |
*/ | |
free(foos); | |
return EXIT_SUCCESS; | |
} | |
This file contains 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
allocated 0x1674070 with value 1 | |
allocated 0x1674090 with value 12 | |
allocated 0x16740b0 with value 23 | |
allocated 0x16740d0 with value 34 | |
allocated 0x16740f0 with value 45 | |
allocated 0x1674110 with value 56 | |
allocated 0x1674130 with value 67 | |
allocated 0x1674150 with value 78 | |
allocated 0x1674170 with value 89 | |
allocated 0x1674190 with value 100 | |
0 1 at 0x1674070 | |
1 12 at 0x1674090 | |
2 23 at 0x16740b0 | |
3 34 at 0x16740d0 | |
4 45 at 0x16740f0 | |
5 56 at 0x1674110 | |
6 67 at 0x1674130 | |
7 78 at 0x1674150 | |
8 89 at 0x1674170 | |
9 100 at 0x1674190 | |
------------------ | |
foo[0]=1 at 0x1674070 | |
foo[1]=23543952 at 0x1674010 | |
Segmentation fault |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment