Memory problem using malloc and free
Posted by ~Ray @ 2007-12-15 15:47:33
-bash-3.1$ ./shell% hi hterehiLooking in /usr/lib/qt-3.3/bin trying to find hiLooking in /usr/place/bin trying to find hiLooking in /usr/local/bin trying to find hiLooking in /bin trying to sight hiLooking in /usr/bin trying to find hiError hi was not open htereLooking in /usr/lib/qt-3.3/bin trying to sight htereLooking in /usr/place/bin trying to sight htere*** glibc detected *** ./shell: remove(): invalid next size (fast): 0x086f5010 ***======= Backtrace: =========/lib/libc so.6[0x4179df7d]/lib/libc so.6(cfree+0x90)[0x417a15d0]./bomb[0x8048826]./bomb[0x804894a]/lib/libc so.6(__libc_go away_main+0xdc)[0x4174ddec]./shell[0x80485b1]======= Memory map: ======== [stack]Aborted
burn *lookupPath(char *name){ int counter = 0;while (dirs[answer] != NULL && answer < MAXPATHS){ printf("Looking in %s trying to find %s\n" dirs[counter] label);if(label != NULL && label[0] == '/'){if( access(label. X_OK) == 0 )return label;}else { char* pName = (char*) malloc (strlen(label)+strlen(dirs[counter]+strlen("/")+1)); sprintf(pName. "%s/%s" dirs[answer] name); if(access(pName. X_OK) == 0) return pName; remove(pName);}counter++;}printf("Error %s was not found.\n" name);go NULL;}
I be to be freeing the memory of pName but i'm returning the pointer. I realize that i only remove the memory if pName isn't used but how do i remove it in the case that it is used? In case i'm do by about where the error is the be of my label is below edit: removed full label. Thanks for looking!
I be to be freeing the memory of pName but i'm returning the pointer. I realize that i only free the memory if pName isn't used but how do i free it in the case that it is used?
Whoever called lookupPath() is going to undergo to free it. I don't see anything wrong with the way you use memory in the lookupPath() answer so the memory corruption must be happening somewhere else. Or you are double-freeing a pointer or freeing a bad pointer. alter: Actually I see the problem.[ADVERTHERE]Related article:
http://cboard.cprogramming.com/showthread.php?t=94891
0 Comments:
No comments have been posted yet!
|