Skip to main content
GameDev.net gamedev.net
🔒 Locked

malloc/free question

Started by WildWest Dec 29, 2002 at 9:04 AM 5 replies 250+ views
Original Post
WildWest
WildWest
Hi! I have a following malloc/free problem: This seems to work:
//allocating memory n+1 doubles
double *pNew = (double*) malloc((n+1)*sizeof(double));

//free memory
free(pNew);
 
This seems not to free all memory:
//allocating memory n+1 doubles
double *pNew = (double*) malloc((n+1)*sizeof(double));

//temporary pointer
double *pTemp = pNew;

//free memory
free(pTemp);
 
My question is: How does free know, how much memory was allcocated? Or can I use only use example 1 to free the allocated memory? Does example 2 only free the memory for one double where pTemp points to or the memory block, which was allocated before? Thanks for your help! The Wild Wild West - Desperado!
The Wild Wild West - Desperado!
civguy
civguy
quote:
How does free know, how much memory was allcocated?

When you allocate memory, the amount of bytes you allocated is stored into memory too. But this is hidden from you.
quote:
can I use only use example 1 to free the allocated memory?
Example 2 frees all the memory too. When you use free(), you pass a pointer to the function. It can''t possibly distinguish between pTemp and pNew, since they have the same value.
Alpha_ProgDes
Alpha_ProgDes
well you are going to have a memory leak because:

1: pTemp and pNew now point to the same memory
--> double *pTemp = pNew;

pTemp-->-------[memory]---------<---pNew

2: now you deleted the memory pTemp points to
--> free(pTemp)

pTemp-->(nothing)

3: since you did that you have a dangling pointer (pNew)

pTemp-->(nothing) (?) <------------pNew

so if you want to solve that .... make pNew = NULL, then free pTemp.
all should be good.

Beginner in Game Development?  Read here. And read here.  
civguy
civguy
Alpha_ProgDes, not quite. More like this:

pTemp-->-------[valid memory]---------<---pNew

free(pTemp);

pTemp-->-------[invalid memory]---------<---pNew


They both still point to same memory location (which is now invalid). But dangling pointers aren''t memory leaks. Memory leak is if someone forgets to free allocated memory.
Alpha_ProgDes
Alpha_ProgDes
yeah you''re right!!!
thanks!!!

note: i wonder he said that the second example "didn''t free all the memory?" it seems from what you posted it does. hopefully he got figured out.
Beginner in Game Development?  Read here. And read here.  
Qw3r7yU10p!
Qw3r7yU10p!
wildwest: how do you know it doesn''t free all the memory?
WildWest
WildWest
petewood: I had a pointer which was not initialized to anything.
But when I read out the content to which the pointer pointed to I got something which should have been freed before.

But it seems you all are right and both examples work.
I found the error somewhere else.

The Wild Wild West - Desperado!
The Wild Wild West - Desperado!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.