Bonjour,

Je revisite un peu le langage C, étant plus connaisseur de la programmation objet C#.

je travaille avec les structure en C sur un petit projet de jeu en SDL (rien de bien exceptionnel). cependant je me pose la question de comment bien rendre la mémoire au système pour éviter les "memory leak".

Voici le bout de code que j'utilise pour déclarer, affecter et utiliser ma structure. J'aimerais savoir si cela est correct en terme de gestion de mémoire particulièrement le dernier volet.

Merci de vos commentaires et de toute aide que vous pourrez me donner.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
//DECLARATION DE MA STRUCTURE 
typedef struct SpriteStruct {
char                     NumerOfLife;
unsigned short           Xlocation;
unsigned short           Ylocation;
char                     Energy;
unsigned short           *XframeLocation;
unsigned short           *YframeLocation;
char                     *Hframe;
char                     *Lframe;
SDL_Surface              *surface;
};
 
//MISE EN PLACE DES TABLEAUX
unsigned short *xframeLocation= (unsigned short *) malloc(10 * sizeof(unsigned short));
unsigned short *yframeLocation= (unsigned short *) malloc(10 * sizeof(unsigned short));
unsigned short *hframe= (unsigned short *) malloc(10 * sizeof(unsigned short)); 
unsigned short *lframe= (unsigned short *) malloc(10 * sizeof(unsigned short));
heroSurface = SDL_LoadBMP("hero.bmp");
 
//INSTANCIATIOn
struct SpriteStruct hero;
 
//AFFECTATION
hero.NumerOfLife=3;
hero.Xlocation=0;
hero.Ylocation=320;
hero.Energy=255;
hero.XframeLocation=xframeLocation;
hero.YframeLocation=yframeLocation;
hero.Hframe=hframe;
hero.Lframe=lframe;
hero.surface=heroSurface;
 
//TRAITEMENTS
......
 
//DESALOCATION
free(hero.XframeLocation);
free(hero.YframeLocation);
free(hero.Hframe);
free(hero.Lframe);
SDL_FreeSurface(hero.surface);
free(hero);