Entendu :)
Sinon, en fait il me retourner cela :
Citation:
*** glibc detected *** ./Life: realloc(): invalid next size: 0x0806ee40 ***
Version imprimable
Entendu :)
Sinon, en fait il me retourner cela :
Citation:
*** glibc detected *** ./Life: realloc(): invalid next size: 0x0806ee40 ***
Voici ta fonction createNewLife() avec quelques modifications (pas testé, car tu n'as pas fourni de code compilable:
ThierryCode:
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 int createNewLife(SDL_Surface* screen, struct life **pp_humans, struct world *p_myWorld) { int error = 0; if (screen != NULL && pp_humans != NULL && *pp_humans != NULL && p_myWorld != NULL) { int nbr = p_myWorld->lifes+1; struct life *ptr = *pp_humans; ptr = realloc(*pp_humans, nbr * sizeof *p_self); if (ptr != NULL) { int r = 0; int g = 0; int b = 0; int x = 0; int y = 0; int c = 254; int p = L_H_CASE * L_H_CASE; SDL_Rect position; p_myWorld->lifes++; ptr[nbr - 1].surface = SDL_CreateRGBSurface(SDL_HWSURFACE, L_H_CASE, L_H_CASE, 32, 0, 0, 0, 0); ptr[nbr - 1].time_life = DELAY/10; ptr[nbr - 1].activate = 0; r = rand() % (c+1); g = rand() % (c+1); b = rand() % (c+1); x = rand() % (p+1); y = rand() % (p+1); ptr[nbr - 1]->position.x = x; position.x = x; ptr[nbr - 1]->position.y = y; position.y = y; while (r == 0 && g == 0 && b == 0) { r = rand() % (c+1); g = rand() % (c+1); b = rand() % (c+1); } ptr[nbr - 1].color.r = r; ptr[nbr - 1].color.g = g; ptr[nbr - 1].color.b = b; SDL_FillRect(ptr[nbr - 1].surface, NULL, SDL_MapRGB(screen->format, r, g, b)); SDL_BlitSurface(ptr[nbr - 1].surface, NULL, screen, &position); /* -tc- on retourne ptr */ *pp_humans = ptr; } else { /* -tc- erreur: la reallocation a echoue */ error = 1; } } else { /* -tc- erreur: argument invalide */ error = 1; } return error; }
Merci :) j'ai corrigé quelques erreurs comme :
...Code:
1
2
3
4
5 if(ptr == NULL) { on continue; }
Sinon ca compile sans problème et je vois mes erreurs mais il y a encore et encore ce fichu :
Merci d'avance :)Code:
1
2 *** glibc detected *** ./Life: realloc(): invalid next size: 0x0806eef0 ***
PS: Je continu d'éplucher le code. Je vais heberger mon code source comme ca si vous le voulez, vous pourrez tester :)
EDIT http://sayro.free.fr/Samy/Prog/Life.tar.gz
Oui, mais elle est surtout inutilement complexe...
http://emmanuel-delahaye.developpez....tes.htm#mallocCode:
1
2 struct life *ptr = realloc (myStruct, nbr * sizeof *ptr);
http://emmanuel-delahaye.developpez....es.htm#realloc
Problème classique et élémentaire :
http://emmanuel-delahaye.developpez....difie_variable
Les pointeurs n'échappent pas à la règle...
Merci :) J'utilise plus calloc mais malloc.
Voilà le code de life.c
J'ai encore le realloc .... next size 0x...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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386 #include <stdio.h> #include <stdlib.h> #include "const.h" #include "life.h" #include "output.h" int createLife(SDL_Surface* screen) { struct life *humans = NULL; struct world myWorld; myWorld.lifes = LIFES; humans = malloc(myWorld.lifes*(sizeof(*humans))); if(humans == NULL) { fprintf(stderr, "Erreur d'allocation de la structure: humans"); return 2; } char message1[32]; int x = 0, y = 0, l = 254, m = L_H_CASE*L_H_CASE, n = 1, s = 5; int x_const = L_WINDOW+10, nbr = 0, size = 10; int r = 0, g = 0, b = 0, people = 0; int time_now = 0, time_before = 0; int distance1 = 0, distance2 = 0; int time_max = DELAY; int world = 1; int i = 0; SDL_Color colorWhite, colorBlack, colorRed, colorGreen; SDL_Surface *sep = NULL; SDL_Rect position; sep = SDL_CreateRGBSurface(SDL_HWSURFACE, 1, 480, 32, 0, 0, 0, 0); if(sep == NULL) { fprintf(stderr, "Erreur d'allocation avec la variable: sep\n"); return 2; } colorWhite.r = 255; colorWhite.g = 255; colorWhite.b = 255; colorBlack.r = 0; colorBlack.g = 0; colorBlack.b = 0; colorRed.r = 255; colorRed.g = 0; colorRed.b = 0; colorGreen.r = 0; colorGreen.g = 255; colorGreen.b = 0; position.x = 0; position.y = 0; people = myWorld.lifes; while(people >= 0) { fprintf(stdout, "\nCreation de l'humain : %i\n", people); (humans+people)->surface = SDL_CreateRGBSurface(SDL_HWSURFACE, L_H_CASE, L_H_CASE, 32, 0, 0, 0, 0); (humans+people)->time_life = DELAY/10; (humans+people)->activate = 0; r = rand() % (l+1); g = rand() % (l+1); b = rand() % (l+1); x = rand() % (m+1); y = rand() % (m+1); (humans+people)->position.x = x; position.x = x; (humans+people)->position.y = y; position.y = y; while(r == 0 && g == 0 && b == 0) { r = rand() % (l+1); g = rand() % (l+1); b = rand() % (l+1); } (humans+people)->color.r = r; (humans+people)->color.g = g; (humans+people)->color.b = b; people--; } people = LIFES; do { sep = SDL_CreateRGBSurface(SDL_HWSURFACE, 1, 480, 32, 0, 0, 0, 0); SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 100, 100, 100)); nbr = 0; size = 10; writeTxt(screen, "Life par Samy Hocine", x_const, 40, size, colorBlack); writeTxt(screen, "Humains: ", x_const, 80, size, colorBlack); sprintf(message1, "%d", myWorld.lifes); writeTxt(screen, message1, x_const+70, 80, size, colorGreen); while(people >= 0) { time_now = SDL_GetTicks(); if((humans+people)->activate == 0) { if((humans+people)->time_life > 0) { if((time_now - time_before) > time_max) /* Si DELAY(ms) se sont e'coule'es */ { time_before = time_now; (humans+people)->time_life = (humans+people)->time_life-1; } i = 0; x = rand() % (n+1); y = rand() % (n+1); distance1 = rand() % (s+1); distance2 = rand() % (s+1); if(x == 1) { /* deplacement vers la droite */ if((humans+people)->position.x < L_WINDOW-L_H_CASE) { (humans+people)->position.x = (humans+people)->position.x+distance1; position.x = (humans+people)->position.x; i++; } else { (humans+people)->position.x = L_WINDOW-L_H_CASE; position.x = (humans+people)->position.x; i++; } } else { /* deplacement vers la gauche */ if((humans+people)->position.x > 0) { (humans+people)->position.x = (humans+people)->position.x-distance2; position.x = (humans+people)->position.x; i++; } } if(y == 1) { /* deplacement vers le bas */ if((humans+people)->position.y < H_WINDOW-L_H_CASE) { (humans+people)->position.y = (humans+people)->position.y+distance1; position.y = (humans+people)->position.y; i++; } else { (humans+people)->position.y = H_WINDOW-L_H_CASE; position.y = (humans+people)->position.y; i++; } } else { /* deplacement vers le haut */ if((humans+people)->position.y > 0) { (humans+people)->position.y = (humans+people)->position.y-distance2; position.y = (humans+people)->position.y; i++; } } int start = 1, other_people = myWorld.lifes, j = 0; while(other_people >= 0 || start == 1) { if((*(humans+other_people)).activate != 1) /* s'il est mort alors rien */ { if((*(humans+other_people)).position.x != (humans+people)->position.x) { start = 0; } else { j++; } if((*(humans+other_people)).position.y != (humans+people)->position.y) { start = 0; } else { j++; } other_people--; } if(j == 2) { int rslt = 0; rslt = createNewLife(screen, &humans, &myWorld); if(rslt != SUCCES) { fprintf(stdout, "Erreur lors de la cre'ation d'un nouvel humain"); return 3; } } } if(i == 2) /* si les deux positions: x et y sont ok alors */ { r = (humans+people)->color.r; g = (humans+people)->color.g; b = (humans+people)->color.b; SDL_FillRect((humans+people)->surface, NULL, SDL_MapRGB(screen->format, r, g, b)); SDL_BlitSurface((humans+people)->surface, NULL, screen, &position); } } else { (humans+people)->activate = 1; position.x = (humans+people)->position.x; position.y = (humans+people)->position.y; (humans+people)->color.r = 0; (humans+people)->color.g = 0; (humans+people)->color.b = 0; r = (humans+people)->color.r; g = (humans+people)->color.g; b = (humans+people)->color.b; SDL_FillRect((humans+people)->surface, NULL, SDL_MapRGB(screen->format, r, g, b)); SDL_BlitSurface((humans+people)->surface, NULL, screen, &position); if(myWorld.lifes != 0) myWorld.lifes--; } } else { position.x = (humans+people)->position.x; position.y = (humans+people)->position.y; (humans+people)->color.r = 0; (humans+people)->color.g = 0; (humans+people)->color.b = 0; r = (humans+people)->color.r; g = (humans+people)->color.g; b = (humans+people)->color.b; SDL_FillRect((humans+people)->surface, NULL, SDL_MapRGB(screen->format, r, g, b)); SDL_BlitSurface((humans+people)->surface, NULL, screen, &position); } people--; } position.x = 500; position.y = 0; SDL_FillRect(sep, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); SDL_BlitSurface(sep, NULL, screen, &position); SDL_Flip(screen); people = LIFES; if(myWorld.lifes <= 0) { world = 0; } } while(world); people = LIFES; while(people >= 0) { free((humans+people)->surface); people--; } free(humans); return SUCCES; } int createNewLife(SDL_Surface* screen, struct life **pp_humans, struct world *p_myWorld) { int error = 0; if (screen != NULL && pp_humans != NULL && *pp_humans != NULL && p_myWorld != NULL) { int nbr = p_myWorld->lifes+1; struct life *ptr = *pp_humans; ptr = realloc(*pp_humans, nbr*(sizeof *ptr)); if (ptr != NULL) { int c = 254, p = L_H_CASE * L_H_CASE; int r = 0, g = 0, b = 0; int x = 0, y = 0; SDL_Rect position; p_myWorld->lifes++; ptr[nbr - 1].surface = SDL_CreateRGBSurface(SDL_HWSURFACE, L_H_CASE, L_H_CASE, 32, 0, 0, 0, 0); ptr[nbr - 1].time_life = DELAY/10; ptr[nbr - 1].activate = 0; r = rand() % (c+1); g = rand() % (c+1); b = rand() % (c+1); x = rand() % (p+1); y = rand() % (p+1); ptr[nbr - 1].position.x = x; position.x = x; ptr[nbr - 1].position.y = y; position.y = y; while (r == 0 && g == 0 && b == 0) { r = rand() % (c+1); g = rand() % (c+1); b = rand() % (c+1); } ptr[nbr - 1].color.r = r; ptr[nbr - 1].color.g = g; ptr[nbr - 1].color.b = b; SDL_FillRect(ptr[nbr - 1].surface, NULL, SDL_MapRGB(screen->format, r, g, b)); SDL_BlitSurface(ptr[nbr - 1].surface, NULL, screen, &position); /* -tc- on retourne ptr */ *pp_humans = ptr; } else { /* -tc- erreur: la reallocation a echoue */ error = 1; } } else { /* -tc- erreur: argument invalide */ error = 1; } return error; }
Merci d'avance :)
J'ai créé un nouveau projet uniquement dans le but d'isoler l'erreur. Voilà le code :
(fonctionne sans SDL... Que du C )
Cependant à l'execution, j'ai :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
90
91
92
93
94
95
96 #include <stdio.h> #include <stdlib.h> #define LIFES 5 struct coor { int x; int y; }; struct life { struct coor coordonnees; int time_life; int activate; }; int createNewLife(struct life **pp_humans); int main(void) { int nbr_lifes = LIFES, i = 0, result = 0; struct life *humans = NULL; humans = malloc(nbr_lifes*(sizeof(humans))); if(humans == NULL) { fprintf(stdout, "Erreur d'allocation me'moire\n"); return EXIT_FAILURE; } while(i < nbr_lifes) { (humans+i)->coordonnees.x = 1; (humans+i)->coordonnees.y = 1; (humans+i)->time_life = 12; (humans+i)->activate = 2; fprintf(stdout, "Humans[%d]: x = %d, y = %d, time = %d, status = %d\n", i, (humans+i)->coordonnees.x, (humans+i)->coordonnees.y, (humans+i)->time_life, (humans+i)->activate = 2); i++; fflush(stdout); } result = createNewLife(&humans); if(result != 0) { if(result == 1) { fprintf(stdout, "[createNewLife]: pp_humans == NULL\n"); return EXIT_FAILURE; } else if(result == 2) { fprintf(stdout, "[createNewLife]: Erreur de reallocation\n"); return EXIT_FAILURE; } } fprintf(stdout, "Test d'allocation de me'moire reussie\n"); fflush(stdout); return EXIT_SUCCESS; } int createNewLife(struct life **pp_humans) { int error = 0, nbr = LIFES; if(*pp_humans != NULL) { struct life *temp_humans = *pp_humans; temp_humans = realloc(*pp_humans, nbr*(sizeof *temp_humans)); if(temp_humans != NULL) { *pp_humans = temp_humans; } else { error = 2; } } else { error = 1; } return error; }
J'ai pourtant suvi vos conseis. J'ai sûrement omis quelque chose, mais quoi...Citation:
*** glibc detected *** ./Test: realloc(): invalid next size: 0x0804a008 ***
Merci d'avance :)
ben d'abord un problème de fond :
tu alloues humans dans le main, tu lui affectes des valeurs, et ensuite tu recommences dans create... (tu écrases le pointeur, tu réalloues, mais sans afecter d evaleur, et de plus tu ne le fais qu'une fois) 8O
et pourquoi pas simplement :
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 int main(void) { int nbr_lifes = 0, i = 0, result = 0; struct life *humans = NULL; while(i <= LIFES ) { if ( createNewLife(&humans, &nbr ) != 0 ) { fprintf(stdout, "Erreur d'allocation me'moire\n"); result = 1 ; } else { fprintf(stdout, "Humans[%d]: x = %d, y = %d, time = %d, status = %d\n", i, (humans+i)->coordonnees.x, (humans+i)->coordonnees.y, (humans+i)->time_life, (humans+i)->activate = 2); } i++; fflush(stdout); } if ( result ) fprintf(stdout, "Une erreur s'est produite lors dutest d'allocation de me'moire\n"); else fprintf(stdout, "Test d'allocation de me'moire reussie\n"); fflush(stout); return EXIT_SUCCESS; } int createNewLife(struct life **pp_humans, int *nbr ) { int error = 0 ; int N ; struct life *temp_humans=NULL; N = *nbr + 1 temp_humans = realloc(*pp_humans, N*sizeof (struct life)); if(temp_humans != NULL) { *pp_humans = temp_humans; (temp_humans+N)->coordonnees.x = 1; (temp_humans+N)->coordonnees.y = 1; (temp_humans+N)->time_life = 12; (temp_humans+N)->activate = 2; *nbr = N ; } else { error = 2; } return error; }
Très bonne initiative, généralement très payante...
Ca déjà, c'est faux. La forme canonique est :Citation:
Code:humans = malloc (nbr_lifes * (sizeof (humans)));
Ton code devrait donc s'écrire :Code:T *p = malloc (n * sizef *p);
Il est inutile de surcharger son code avec des parenthèses inutiles. Il est par contre beaucoup plus utile d'écrire une syntaxe correcte.Code:humans = malloc (nbr_lifes * sizeof *human);
Le bloc alloué n'est jamais libéré...
Merci à vous, je vais essayer de voir ce week end car j'ai le bac blanc de français.
Sinon, j'ai effectivement oublié de mettre free à la fin.
Tout d'abord merci, j'ai fait un code plus simple qu'au début comme on me l'a proposé :
En ce qui concerne la question posée à l'utilisateur, je n'ai pas besoin de vérifier si les données sont correctement entrées (voir si c'est un nombre ou un caractère, utiliser fclean...) car dans mon vrai projet, l'utilisateur ne touchera pas au logiciel. C'est donc uniquement moi, le programmeur qui va répondre à cette question et donc je ne vais pas faire n'importe quoi.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 #include <stdio.h> #include <stdlib.h> #define LIFES 5 struct coor { int x; int y; }; struct life { struct coor coordonnees; int time_life; int activate; }; struct world { int nbr_of_lifes; }; int createNewLife(struct life **pp_humans, struct world *aWorld); int main(void) { int result = 0, i = 0, j = 0; char rep[8]; struct life *humans = NULL; struct world myWorld; myWorld.nbr_of_lifes = LIFES; humans = malloc(myWorld.nbr_of_lifes*sizeof *humans); if(humans == NULL) { fprintf(stdout, "Erreur d'allocation me'moire\n"); return EXIT_FAILURE; } while(i <= LIFES) { result = createNewLife(&humans, &myWorld); if(result != 0) { if(result == 1) { fprintf(stdout, "[createNewLife]: pp_humans == NULL\n"); return EXIT_FAILURE; } else if(result == 2) { fprintf(stdout, "[createNewLife]: Erreur de reallocation\n"); return EXIT_FAILURE; } else if(result == 3) { fprintf(stdout, "[createNewLife]: aWorld == NULL\n"); return EXIT_FAILURE; } } else { fprintf(stdout, "Humans[%d]: x = %d, y = %d, time = %d, status = %d\n", i, (humans+i)->coordonnees.x, (humans+i)->coordonnees.y, (humans+i)->time_life, (humans+i)->activate); } i++; } i = 0; fprintf(stdout, "Combien voulez vous faire encore d'humains ?\nRe'ponse: "); fgets(rep, sizeof rep, stdin); j = atoi(rep); while(i <= j) { result = createNewLife(&humans, &myWorld); if(result != 0) { if(result == 1) { fprintf(stdout, "[createNewLife]: pp_humans == NULL\n"); return EXIT_FAILURE; } else if(result == 2) { fprintf(stdout, "[createNewLife]: Erreur de reallocation\n"); return EXIT_FAILURE; } else if(result == 3) { fprintf(stdout, "[createNewLife]: aWorld == NULL\n"); return EXIT_FAILURE; } } else { fprintf(stdout, "Humans[%d]: x = %d, y = %d, time = %d, status = %d\n", i, (humans+i)->coordonnees.x, (humans+i)->coordonnees.y, (humans+i)->time_life, (humans+i)->activate); } i++; } fprintf(stdout, "Test d'allocation de me'moire reussie\n"); fflush(stdout); free(humans); return EXIT_SUCCESS; } int createNewLife(struct life **pp_humans, struct world *aWorld) { int error = 0, n = 0; n = aWorld->nbr_of_lifes + 1; if(aWorld != NULL) { if(*pp_humans != NULL) { struct life *temp_humans = *pp_humans; temp_humans = realloc(*pp_humans, n*sizeof *temp_humans); if(temp_humans != NULL) { *pp_humans = temp_humans; (temp_humans+n)->coordonnees.x = 89; (temp_humans+n)->coordonnees.y = 92; (temp_humans+n)->time_life = 10; (temp_humans+n)->activate = 0110; aWorld->nbr_of_lifes = n; } else { error = 2; } } else { error = 1; } } else { error = 3; } return error; }
Sinon, voilà ce qu'il me retourne : (le code compile)
Il y a quelque chose de faux car les valeurs affichées ne sont pas celles que j'ai indiqué dans le code. Je ne vois pas, sûrement au niveau de realloc ou de :Citation:
samsoft@samsoft-laptop:~/Programmes/C/Test/bin/Release$ ./Test
Humans[0]: x = 0, y = 0, time = 0, status = 0
Humans[1]: x = 0, y = 0, time = 0, status = 57
Humans[2]: x = 10, y = 135041, time = 0, status = 0
Humans[3]: x = 0, y = 0, time = 0, status = 0
Humans[4]: x = 0, y = 0, time = 0, status = 0
Humans[5]: x = 0, y = 0, time = 0, status = 0
Combien voulez vous faire encore d'humains ?
Re'ponse: 2
Humans[0]: x = 0, y = 0, time = 0, status = 0
Humans[1]: x = 0, y = 0, time = 0, status = 92
Humans[2]: x = 10, y = 135041, time = 0, status = 135033
Test d'allocation de me'moire reussie
samsoft@samsoft-laptop:~/Programmes/C/Test/bin/Release$
"monPtr = monAutrePtr".
Merci d'avance :)
PS: J'ai bien remarqué la petite erreur : si on demande 2 il en sort 3, je vais corriger.
je ne comprend toujours pas pourquoi tu tiens a creer des humans AVANT de poser la question combien il en faut.....
D'autre part tes chiffres sont faux, car tu te sers encore de pointeurs la ou c'est des valeurs...
En fait dans mon vrai programme, au début, on a une création d'un certains nombres d'humains en gros : "Adam et Eve", le nombre ici est constant et ensuite, on a la reproduction qui provoque la création de plusieurs autres humains, le nombre est ici aléatoire et est croissant au cours du temps. Sinon pour le reste, je vais voir ca demain.
Merci :)
Erreur d'indexation qui fait aller en dehors du tableau allouéCode:
1
2
3
4
5
6
7
8
9
10
11 temp_humans = realloc(*pp_humans, n*sizeof *temp_humans); if(temp_humans != NULL) { *pp_humans = temp_humans; (temp_humans+n-1)->coordonnees.x = 89; (temp_humans+n-1)->coordonnees.y = 92; (temp_humans+n-1)->time_life = 10; (temp_humans+n-1)->activate = 0110; aWorld->nbr_of_lifes = n; }
Merci, je viens de voir cela. Malheureusement, il reste encore un dernier problème. Seul le dernier élément du tableau est correct :
Voilà mon code :Citation:
Humans[0]: x = 0, y = 0, time = 0, status = 0
Humans[1]: x = 0, y = 0, time = 0, status = 0
Humans[2]: x = 0, y = 0, time = 0, status = 0
Humans[3]: x = 0, y = 0, time = 0, status = 0
Humans[4]: x = 0, y = 0, time = 0, status = 0
Humans[5]: x = 89, y = 92, time = 10, status = 72
J'imprime en ce moment mon code source pour y voir plus clair.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 #include <stdio.h> #include <stdlib.h> #define LIFES 5 struct coor { int x; int y; }; struct life { struct coor coordonnees; int time_life; int activate; }; struct world { int nbr_of_lifes; }; int createNewLife(struct life **pp_humans, struct world *aWorld); int main(void) { int result = 0, i = 0, j = 0; char rep[128]; struct life *humans = NULL; struct world myWorld; myWorld.nbr_of_lifes = LIFES; humans = malloc(myWorld.nbr_of_lifes*sizeof *humans); if(humans == NULL) { fprintf(stdout, "Erreur d'allocation me'moire\n"); return EXIT_FAILURE; } while(i <= LIFES) { result = createNewLife(&humans, &myWorld); if(result != 0) { if(result == 1) { fprintf(stdout, "[createNewLife]: pp_humans == NULL\n"); return EXIT_FAILURE; } else if(result == 2) { fprintf(stdout, "[createNewLife]: Erreur de reallocation\n"); return EXIT_FAILURE; } else if(result == 3) { fprintf(stdout, "[createNewLife]: aWorld == NULL\n"); return EXIT_FAILURE; } } else { fprintf(stdout, "Humans[%d]: x = %d, y = %d, time = %d, status = %d\n", i, (humans+i)->coordonnees.x, (humans+i)->coordonnees.y, (humans+i)->time_life, (humans+i)->activate); } i++; } i = 1; fprintf(stdout, "Combien voulez vous faire encore d'humains ?\nRe'ponse: "); fgets(rep, sizeof rep, stdin); j = atoi(rep); while(i <= j) { result = createNewLife(&humans, &myWorld); if(result != 0) { if(result == 1) { fprintf(stdout, "[createNewLife]: pp_humans == NULL\n"); return EXIT_FAILURE; } else if(result == 2) { fprintf(stdout, "[createNewLife]: Erreur de reallocation\n"); return EXIT_FAILURE; } else if(result == 3) { fprintf(stdout, "[createNewLife]: aWorld == NULL\n"); return EXIT_FAILURE; } } else { fprintf(stdout, "Humans[%d]: x = %d, y = %d, time = %d, status = %d\n", i, (humans+i)->coordonnees.x, (humans+i)->coordonnees.y, (humans+i)->time_life, (humans+i)->activate); } i++; } fprintf(stdout, "Test d'allocation de me'moire reussie\n"); fflush(stdout); free(humans); return EXIT_SUCCESS; } int createNewLife(struct life **pp_humans, struct world *aWorld) { int error = 0, n = 0; n = aWorld->nbr_of_lifes + 1; if(aWorld != NULL) { if(*pp_humans != NULL) { struct life *temp_humans = *pp_humans; temp_humans = realloc(*pp_humans, n*sizeof *temp_humans); if(temp_humans != NULL) { *pp_humans = temp_humans; (temp_humans+n-1)->coordonnees.x = 89; (temp_humans+n-1)->coordonnees.y = 92; (temp_humans+n-1)->time_life = 10; (temp_humans+n-1)->activate = 0110; aWorld->nbr_of_lifes = n; } else { error = 2; } } else { error = 1; } } else { error = 3; } return error; }
Merci d'avance :)
Absolument normal :P
Tu démarres en settant le nombre de vies à 5, et dans CreateNewLife tu assignes le n comme nombre de vies + 1... :aie:
Je répète que tu n'as pas besoin de faire le premier malloc...
Voici ce que je te propose :
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158 #include <stdio.h> #include <stdlib.h> #define LIFES 5 typedef struct pcoor { int x; int y; } coor ; typedef struct plife { coor coordonnees; int time_life; int activate; } life ; typedef struct pworld { int nbr_of_lifes; } world ; int createNewLife(life **pp_humans, world *aWorld); int main(void) { int result = 0, i = 0, j = 0 ; char rep[128]; life *humans = NULL; world myWorld; int k, TotalResult=0 ; myWorld.nbr_of_lifes = LIFES; while(i <= LIFES) { result = createNewLife(&humans, &myWorld); if(result != 0) { if(result == 2) { fprintf(stderr, "[createNewLife]: Erreur de reallocation\n"); } else { fprintf(stderr, "[createNewLife]: aWorld == NULL\n"); } TotalResult = result ; } else { k = myWorld.nbr_of_lifes - 1 ; fprintf(stdout, "Humans[%d]: x = %d, y = %d, time = %d, status = %d\n", k, humans[k].coordonnees.x, humans[k].coordonnees.y, humans[k].time_life, humans[k].activate); } i++; } i = 1; fprintf(stdout, "Combien voulez vous faire encore d'humains ?\nRe'ponse: "); fgets(rep, sizeof rep, stdin); j = atoi(rep); while(i <= j) { result = createNewLife(&humans, &myWorld); if(result != 0) { if(result == 2) { fprintf(stderr, "[createNewLife]: Erreur de reallocation\n"); } else { fprintf(stderr, "[createNewLife]: aWorld == NULL\n"); } TotalResult = result ; } else { k = myWorld.nbr_of_lifes - 1 ; fprintf(stdout, "Humans[%d]: x = %d, y = %d, time = %d, status = %d\n", k, humans[k].coordonnees.x, humans[k].coordonnees.y, humans[k].time_life, humans[k].activate); } i++; } if (TotalResult == 0 ) { fprintf(stdout, "Test d'allocation de me'moire reussie\n"); fflush(stdout); free(humans); return EXIT_SUCCESS; } else { fprintf(stdout, "Des erreurs d'allocation me'moire se sont produites\n"); fflush(stdout); if ( humans != NULL ) free(humans); return EXIT_FAILURE; } } int createNewLife(life **pp_humans, world *aWorld) { int error = 0, n = 0; n = aWorld->nbr_of_lifes + 1; if(aWorld != NULL) { life *temp_humans = NULL ; /* * ici si on veut (mais le realloc gère NULL depuis C89), on peut faire * if ( *pp_humans == NULL ) * *pp_humans = malloc ( sizeof(life) ): * else * temp_humans = realloc(*pp_humans, n*sizeof *temp_humans); */ temp_humans = realloc(*pp_humans, n*sizeof *temp_humans); if(temp_humans != NULL) { *pp_humans = temp_humans; (temp_humans+n-1)->coordonnees.x = 89; (temp_humans+n-1)->coordonnees.y = 92; (temp_humans+n-1)->time_life = 10; (temp_humans+n-1)->activate = 0110; aWorld->nbr_of_lifes = n; } else { error = 2; } } else { error = 1; } return error; }
Ce que j'ai fait :
- D'abord mettre des typedef pour ne pas avoir à écrire "struct toto" à chaque fois
- Ensuite éliminer l'allocation inutile de humans au départ (avec initialisation à NULL).
- Ensuite modifier de manière correspondante le createnew pour enlever le test inutile
- Ensuite simplifier la gestion d'erreurs et te faire un programme "error proof", qui continue même si temporairement tu as une erreur de mémoire.
- Ensuite échanger stdout pour stderr quand une erreur arrive (c'est fait pour)
- Ensuite référencer (par k) les humans en fonction du VRAI nombre présent dans la structure, et non par i, qui est indépendant des erreurs d'allocations.
- Enfin garder une trace (TotalResult) de savoir si une erreur s'est produite à un moment donné pour prendre une action correcte à la fin (une erreur ne signifie pas que l'on doive terminer tout).
ça devrait te simplifier la vie... ;)
Je vais voir ca cette semaine et merci à vous tous :)
[hs]
Pour le moment mon esprit est ailleurs : "commande sur le net mon futur robot programmable [ma-vin]"
[/hs]
Après avoir modifié quelque truc pour l'insérer dans mon vrai programme, je constate que ca fonctionne super bien :)
Merci à tous et pour tout !