943,865 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 970
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
May 20th, 2009
0

Problem with free()

Expand Post »
code is :

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

whats wrong with this?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Pramoda.M.A is offline Offline
5 posts
since Oct 2008
May 20th, 2009
0

Re: Problem with free()

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!!!
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
May 20th, 2009
0

Re: Problem with free()

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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
May 20th, 2009
0

Re: Problem with free()

Thank AIa, but I got confusion about the statement you wrote.
Quote ...
it is not recomened to cast malloc, just include stdlib.h and it'll know what to do ?
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
May 20th, 2009
1

Re: Problem with free()

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.
Reputation Points: 22
Solved Threads: 11
Junior Poster
fpmurphy is offline Offline
144 posts
since Oct 2008
May 20th, 2009
0

Re: Problem with free()

adatapost> Thank AIa, but I got confusion about the statement you wrote.
Explanations of ... >casting malloc
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
May 20th, 2009
0

Re: Problem with free()

Click to Expand / Collapse  Quote originally posted by Aia ...
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);
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
May 20th, 2009
0

Re: Problem with free()

Click to Expand / Collapse  Quote originally posted by adatapost ...
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
May 20th, 2009
0

Re: Problem with free()

Buddy Aia,

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

Quote ...
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 adatapost; Dec 15th, 2010 at 3:07 am.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
May 20th, 2009
0

Re: Problem with free()

> 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".
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Debugging using gdb
Next Thread in C Forum Timeline: Parsing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC