Problem with free()

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 5
Reputation: Pramoda.M.A is an unknown quantity at this point 
Solved Threads: 0
Pramoda.M.A Pramoda.M.A is offline Offline
Newbie Poster

Problem with free()

 
0
  #1
May 20th, 2009
code is :

code is :
path = (char*)malloc(100);
pname = (char*)malloc(100);
path = "ragHu";
strcpy(pname, path);
free(path);

whats wrong with this?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,621
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 470
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Problem with free()

 
0
  #2
May 20th, 2009
Originally Posted by Pramoda.M.A View Post
code is :

code is :
path = (char*)malloc(100);
pname = (char*)malloc(100);
path = "ragHu";
strcpy(pname, path);
free(path);

whats wrong with this?
  1. path = (char*)malloc(100);
statement allocates heap and assign base address to the path if allocation is success. (we assume that the allocation is succeed)

  1. path = "ragHu";
- New heap is prepared to store "ragHu" and it base address of newly created heap is assigned.

and finally,
  1. strcpy(pname, path);
  2. free(path);

It's a memory leak!!!
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Problem with free()

 
0
  #3
May 20th, 2009
Pramoda.M.A> whats wrong with this?

  1. path = (char*)malloc(100); /* it is not recomened to cast malloc, just include stdlib.h and it'll know what to do */
  2. /* malloc returns NULL when it can not allocate the requested memory, checking the return is a must */
  3.  
  4. pname = (char*)malloc(100);
  5. /* same advise than for previous statement */
  6.  
  7. path = "ragHu";
  8. /* re-assigning path to a literal string, makes the previous malloced memory to be in limbo, you don't have any way of accessing it back or freeing it */
  9.  
  10. strcpy(pname, path);
  11. /* what happens if there's no memory in pname to hold path? */
  12.  
  13. free(path);
  14. /* path doesn't point to any memory that needs to be freed */
  15. /* pname needs to be freed at some point */
Last edited by Aia; May 20th, 2009 at 4:09 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,621
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 470
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Problem with free()

 
0
  #4
May 20th, 2009
Thank AIa, but I got confusion about the statement you wrote.
it is not recomened to cast malloc, just include stdlib.h and it'll know what to do ?
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 95
Reputation: fpmurphy is an unknown quantity at this point 
Solved Threads: 5
fpmurphy fpmurphy is offline Offline
Junior Poster in Training

Re: Problem with free()

 
1
  #5
May 20th, 2009
The problem is with the line
  1. path = "ragHu";

It should be replaced with the following
  1. strcpy(path, "ragHu");
Last edited by fpmurphy; May 20th, 2009 at 4:27 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Problem with free()

 
0
  #6
May 20th, 2009
adatapost> Thank AIa, but I got confusion about the statement you wrote.
Explanations of ... >casting malloc
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,621
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 470
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Problem with free()

 
0
  #7
May 20th, 2009
Originally Posted by Aia View Post
adatapost> Thank AIa, but I got confusion about the statement you wrote.
Explanations of ... >casting malloc
adatapost> Link Explanations of ... >casting malloc has inappropriate explanation of malloc function.

originally, the prototype of malloc function is :
  1. void *malloc(size_t size);
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Problem with free()

 
0
  #8
May 20th, 2009
Originally Posted by adatapost View Post
adatapost> Link Explanations of ... >casting malloc has inappropriate explanation of malloc function.

originally, the prototype of malloc function is :
  1. void *malloc(size_t size);
You are in error. There was not such a thing as a void type at the inception of the C language. Read the explanation piece again, if you must.
Last edited by Aia; May 20th, 2009 at 12:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,621
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 470
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Problem with free()

 
0
  #9
May 20th, 2009
Buddy Aia,

I have strong reason to say the return type of malloc is void. Following two line from MSDN are :

A pointer to void can be converted to or from a pointer to any type, without restriction or loss of information. If the result is converted back to the original type, the original pointer is recovered.
Last edited by Ancient Dragon; May 20th, 2009 at 8:10 pm. Reason: replaced txt tags with quote tags
Attached Thumbnails
exp1.PNG  
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Problem with free()

 
0
  #10
May 20th, 2009
> I have strong reason to say the return type of malloc is void. Following two line from MSDN are :
The key word being "is".
Which isn't the same as "was" or "always has been".
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC