question about calloc() and free()
ok , lets asy i got this structure:
struct PARTICLE_GROUP {
PARTICLE *particles; // Dynamic array of particles
double lifetime;
double lifetime_left; // Does we have to free the array?
COLOR_STEP *cs_top;
double color_trans_timeleft;
double size_trans_timeleft;
D3DCOLORVALUE current_color;
float current_width;
float current_height;
PARTICLE_GROUP *next; // The next group
};
and im allocating memory for it like that:
PARTICLE_GROUP *pg;
pg = (PARTICLE_GROUP *)calloc(1,sizeof(PARTICLE_GROUP));
ok , so now , can i free the allocated block like that?:
PARTICLE_GROUP *pg2;
pg2 = pg;
free(pg2);
well , can i do it?
is it good?
coz , seems that im having problems with it in my particle engine .
thanks all.
*************************
"Everything you know is wrong" - Bono
*************************
------------------------------- Goblineye Entertainment------------------------------
Make sure pg is not NULL. You can''t free a NULL pointer.
------------------------------------------------DaWizardhttp://www.geocities.com/ahmadkabani------------------------------------------------
quote: Original post by Xeno
and im allocating memory for it like that:
PARTICLE_GROUP *pg;
pg = (PARTICLE_GROUP *)calloc(1,sizeof(PARTICLE_GROUP));
BTW, you should swap the function arguments, and do
PARTICLE_GROUP *pg;pg = (PARTICLE_GROUP *)calloc(sizeof(PARTICLE_GROUP), 1);
quote: Original post by Xeno
ok , so now , can i free the allocated block like that?:
PARTICLE_GROUP *pg2;
pg2 = pg;
free(pg2);
well , can i do it?
is it good?
Yes, you can do it but you DON''T NEED to duplicate the pointer to the allocate heap chunk to free it. It''s not a good practice since you are creating TWO dangling references.
quote: Original post by Xeno
coz , seems that im having problems with it in my particle engine .
It depends on what is the the problem. Sincerly, I don''t think the problem may reside here.
[home page] [e-mail]
---
"Lifting shadows off a dream once broken
She can turn a drop of water into an ocean"
---[home page] [[email=karmalaa@inwind.it]e-mail[/email]]
quote: Original post by DaWizard
Make sure pg is not NULL. You can''t free a NULL pointer.
free()ing a NULL pointer is required by the standard to be safe.
ANSI Sec 7.20.3.2:
"The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs."
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement