Ik ben bezig met een programma en daar heb ik meerdere array van pointers naar structs nodig, maar op 1 of andere manier heb ik vage problemen.
Hier een stukje code wat ik ongeveer doe:
Als ik het onder freebsd compile (gcc -ggdb -Wall test.c) dan gaat het aanmaken van de structs etc allemaal prima, maar als ik eenmaal alles nog is wil doorlopen (laatste paar regels in de code) zijn er opeens pointers NULL (in dit geval heel vaag 4,8,16,32,etc)
En als ik het onder linux compile segfault ie bij het aanmaken van post 4 (die bij freebsd ergens opeens NULL word). Gdb output:
Ik doe vast iets heel stoms fout, maar ik heb echt geen idee wat
Hier een stukje code wat ik ongeveer doe:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
| #include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct s_item_info
{
char id[10];
} _item_info ;
typedef struct s_post
{
_item_info **items;
int id;
} _post;
struct s_posts
{
_post **post;
};
int main()
{
_post *post;
_item_info *item;
_post **n_post;
struct s_posts posts;
int i,j;
int imax = 300;
int jmax = 2;
posts.post = NULL;
// Posts aanmaken
for( i = 0; i < imax; i++ )
{
// Ruimte voor de post
post = (_post *)malloc( sizeof(_post) );
assert( post != NULL );
// Ruimte maken voor de pointers naar de items
post->items = (_item_info **)malloc( sizeof(_item_info *) * jmax );
post->id = i;
assert(post->items != NULL);
printf("Creating items for post[%d]\n", i);
// Items aanmaken
for( j = 0; j < jmax; j++ )
{
// Ruimte:
item = (_item_info *)malloc( sizeof(_item_info) );
assert(item != NULL);
sprintf(item->id, "[%d][%d]", i,j);
// Ref van de item opslaan in de post
post->items[ j ] = item;
}
// Ref van de post opslaan in de posts struct
n_post = (_post **)realloc(posts.post, sizeof(_post *) * i);
assert(n_post != NULL);
posts.post = n_post;
posts.post[ i ] = post;
if(posts.post[ i ] == NULL)
{
printf("posts.post[%d] == NULL\n", i);
exit(EXIT_FAILURE);
}
printf("posts.post[ %d ] = post\n", i);
}
for( i = 0; i < imax; i++ )
{
if(posts.post[ i ] == NULL)
printf("posts.post[%d] == NULL\n", i);
else
{
for( j = 0; j < jmax; j++ )
{
printf("[%d][%d] vs %s\n",
i, j, posts.post[ i ]->items[ j ]->id);
}
}
}
exit(EXIT_SUCCESS);
} |
Als ik het onder freebsd compile (gcc -ggdb -Wall test.c) dan gaat het aanmaken van de structs etc allemaal prima, maar als ik eenmaal alles nog is wil doorlopen (laatste paar regels in de code) zijn er opeens pointers NULL (in dit geval heel vaag 4,8,16,32,etc)
En als ik het onder linux compile segfault ie bij het aanmaken van post 4 (die bij freebsd ergens opeens NULL word). Gdb output:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| Creating items for post[0] posts.post[ 0 ] = post Creating items for post[1] posts.post[ 1 ] = post Creating items for post[2] posts.post[ 2 ] = post Creating items for post[3] posts.post[ 3 ] = post Creating items for post[4] Program received signal SIGSEGV, Segmentation fault. 0x4006e7ff in realloc () from /lib/libc.so.6 (gdb) bt #0 0x4006e7ff in realloc () from /lib/libc.so.6 #1 0x4006e78d in realloc () from /lib/libc.so.6 #2 0x8048612 in main () at test.c:59 |
Ik doe vast iets heel stoms fout, maar ik heb echt geen idee wat