| | |
memory leak
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
> my idea is to find whether there is a free() for every malloc before exiting the function.
Is this a memory leak?
Is this a memory leak?
C Syntax (Toggle Plain Text)
void foo ( char *msg ) { char *p = malloc( 10 ); if ( strlen(msg) >= 10 ) { free( p ); p = NULL; } else { strcpy( p, msg ); } return p; } int main ( ) { char *p = foo( "hello" ); if ( p ) { printf( "Yay %s\n", p ); free( p ); } return 0; }
One way to do it is to write your own malloc() and free() functions which keep track of the allocated pointers in a linked list. Then just before program exit run through the linked list to find the pointers that have not been freed.
C Syntax (Toggle Plain Text)
struct node { unsigned int line_number; char* file; void* ptr; struct node* next; }; void* mymalloc(unsigned int size, unsigned ine line_number, const char*filename) { void* ptr = malloc(size); // now add the pointer to the linked list, not shown return ptr; } void myfree(void* ptr) { // search list for ptr and when (and if) found delete the node // code is not shown here free(ptr); } int main() { // example use. Note: I don't know if all compilers support __LINE__ and // __FILE__ macros or not. __LINE__ is a preprocessor directive that // returns the current line number of the file being compiled, and // __FILE__ is a preprocessor directive that returns the file name // of the current file being compiled. int* array = mymalloc( sizeof(int * 10), __LINE__, __FILE__); }
Last edited by Ancient Dragon; Aug 9th, 2008 at 1:07 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Aug 2008
Posts: 5
Reputation:
Solved Threads: 0
Thanks for the reply. But my requirement was different. This code works fine when you want to write new program. What i want is to find a missing free() in existing code. In a big application, you come across the below sub sections. Then how do you deal with it ? there needs to be a memory trailer (as a part of debugging)to check whether memory was freed before exiting.
C Syntax (Toggle Plain Text)
void foo ( char *msg ) { char *p = malloc( 10 ); if ( strlen(msg) >= 10 ) -> at this point if free was missed ? return; else { strcpy( p, msg ); } return p; } int main ( ) { char *p = foo( "hello" ); if ( p ) { printf( "Yay %s\n", p ); free( p ); } return 0; }
•
•
•
•
> my idea is to find whether there is a free() for every malloc before exiting the function.
Is this a memory leak?
C Syntax (Toggle Plain Text)
void foo ( char *msg ) { char *p = malloc( 10 ); if ( strlen(msg) >= 10 ) { free( p ); p = NULL; } else { strcpy( p, msg ); } return p; } int main ( ) { char *p = foo( "hello" ); if ( p ) { printf( "Yay %s\n", p ); free( p ); } return 0; }
Last edited by Ancient Dragon; Aug 9th, 2008 at 6:46 pm. Reason: add code tags
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Is this an assignment, just to go through and count occurrences of these particular functions within some *.c file?
I ask, because for most "REAL WORLD" programs you can't just merely write a script that counts the occurrences of malloc with the occurances of free and check that they're equal.
real world programs are not always nice and neat with a free following every malloc like how you expect HTML files always to always have a "close tag" occuring somewhere inline after its corresponding "open tag".
typically memory leaks occur because the program logic often calls functions or conditionally executes blocks of codes that contain either a malloc or a free -- but not both -- and the programmer misses some unexpected condition in the logic and therefore makes (or forgets) an unaccounted call to malloc or free.
memory allocation problems is the bane of program debugging. there are far too many programmers out there who think they're somehow "cool" because they use malloc, even though 99% of the time they don't need to.
combine this with the fact that many of these characters tend to write sloppy, convoluted code, and you'll begin to understand Why I F-ing Hate Malloc
.
I ask, because for most "REAL WORLD" programs you can't just merely write a script that counts the occurrences of malloc with the occurances of free and check that they're equal.
real world programs are not always nice and neat with a free following every malloc like how you expect HTML files always to always have a "close tag" occuring somewhere inline after its corresponding "open tag".
typically memory leaks occur because the program logic often calls functions or conditionally executes blocks of codes that contain either a malloc or a free -- but not both -- and the programmer misses some unexpected condition in the logic and therefore makes (or forgets) an unaccounted call to malloc or free.
memory allocation problems is the bane of program debugging. there are far too many programmers out there who think they're somehow "cool" because they use malloc, even though 99% of the time they don't need to.
combine this with the fact that many of these characters tend to write sloppy, convoluted code, and you'll begin to understand Why I F-ing Hate Malloc
.
Last edited by jephthah; Aug 12th, 2008 at 6:50 pm.
![]() |
Similar Threads
- Memory leak in game (C++)
- SDL library memory leak (C)
- Looking for Memory Leak Advice (C++)
- Can't prevent memory leak (C++)
- can't find my memory leak (C++)
- Memory Leak? (C)
- Constructor and Convertion operator are the same,how to avoid memory leak? (C++)
Other Threads in the C Forum
- Previous Thread: fibonacci
- Next Thread: Reading text file ...
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h







...