| | |
Problem with free()
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
•
•
code is :
code is :
path = (char*)malloc(100);
pname = (char*)malloc(100);
path = "ragHu";
strcpy(pname, path);
free(path);
whats wrong with this?
C Syntax (Toggle Plain Text)
path = (char*)malloc(100);
C Syntax (Toggle Plain Text)
path = "ragHu";
and finally,
C Syntax (Toggle Plain Text)
strcpy(pname, path); free(path);
It's a memory leak!!!
Failure is not fatal, but failure to change might be. - John Wooden
Pramoda.M.A> whats wrong with this?
c Syntax (Toggle Plain Text)
path = (char*)malloc(100); /* it is not recomened to cast malloc, just include stdlib.h and it'll know what to do */ /* malloc returns NULL when it can not allocate the requested memory, checking the return is a must */ pname = (char*)malloc(100); /* same advise than for previous statement */ path = "ragHu"; /* 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 */ strcpy(pname, path); /* what happens if there's no memory in pname to hold path? */ free(path); /* path doesn't point to any memory that needs to be freed */ /* pname needs to be freed at some point */
Last edited by Aia; May 20th, 2009 at 4:09 am.
•
•
Join Date: Oct 2008
Posts: 95
Reputation:
Solved Threads: 5
The problem is with the line
It should be replaced with the following
C Syntax (Toggle Plain Text)
path = "ragHu";
It should be replaced with the following
C Syntax (Toggle Plain Text)
strcpy(path, "ragHu");
Last edited by fpmurphy; May 20th, 2009 at 4:27 am.
adatapost> Thank AIa, but I got confusion about the statement you wrote.
Explanations of ... >casting malloc
Explanations of ... >casting malloc
•
•
•
•
adatapost> Thank AIa, but I got confusion about the statement you wrote.
Explanations of ... >casting malloc
originally, the prototype of malloc function is :
C Syntax (Toggle Plain Text)
void *malloc(size_t size);
Failure is not fatal, but failure to change might be. - John Wooden
•
•
•
•
adatapost> Link Explanations of ... >casting malloc has inappropriate explanation of malloc function.
originally, the prototype of malloc function is :
C Syntax (Toggle Plain Text)
void *malloc(size_t size);
Last edited by Aia; May 20th, 2009 at 12:50 pm.
Buddy Aia,
I have strong reason to say the return type of malloc is void. Following two line from MSDN are :
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
Failure is not fatal, but failure to change might be. - John Wooden
![]() |
Similar Threads
- Need free hosting for a game server (Web Hosting Deals)
- problem with deleting node in linked list (C)
- Class array inside of a class array inside of a class problem (C++)
- Am I virus / problem free? (Viruses, Spyware and other Nasties)
- which free webhosting site supports asp? (HTML and CSS)
- free () problem (C++)
- Thanks, Still Trouble Free! (Viruses, Spyware and other Nasties)
- i.e. 6 problem (Web Browsers)
Other Threads in the C Forum
- Previous Thread: Debugging using gdb
- Next Thread: Parsing
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks bash binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram ide inches include infiniteloop initialization input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation overwrite pdf pointer pointers posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming standard strchr string suggestions systemcall test testautomation testing threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






