Thread: Memory problem using malloc and free

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    7

    Memory problem using malloc and free

    Hello,

    I'm writing the great shell repacement program. I am having a problem in that i get this error:

    -bash-3.1$ ./shell
    % hi htere
    hiLooking in /usr/lib/qt-3.3/bin trying to find hi
    Looking in /usr/place/bin trying to find hi
    Looking in /usr/local/bin trying to find hi
    Looking in /bin trying to find hi
    Looking in /usr/bin trying to find hi
    Error hi was not found.
    htereLooking in /usr/lib/qt-3.3/bin trying to find htere
    Looking in /usr/place/bin trying to find htere
    *** glibc detected *** ./shell: free(): invalid next size (fast): 0x086f5010 ***
    ======= Backtrace: =========
    /lib/libc.so.6[0x4179df7d]
    /lib/libc.so.6(cfree+0x90)[0x417a15d0]
    ./shell[0x8048826]
    ./shell[0x804894a]
    /lib/libc.so.6(__libc_start_main+0xdc)[0x4174ddec]
    ./shell[0x80485b1]
    ======= Memory map: ========
    [stack]
    Aborted
    i trace the error to this section of my code
    Code:
    char *lookupPath(char *name)
    {
      int counter = 0;
    	while (dirs[counter] != NULL && counter < MAXPATHS)
    	{    
    		printf("Looking in %s trying to find %s\n"
    		  , dirs[counter], name);
    
    		if(name != NULL && name[0]  == '/')
    		{
    			if( access(name, X_OK) == 0 )
    				return name;
    		}
    		else 
    		{	  
    		  char* pName = (char*) malloc (strlen(name)+strlen(dirs[counter]+strlen("/")+1));
    		  sprintf(pName, "%s/%s", dirs[counter], name);
    		  if(access(pName, X_OK) == 0)
    		    return pName;
    		  free(pName);
    		}
    		counter++;
    	}
    	printf("Error %s was not found.\n", name);
    	return NULL;
    }
    I need 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? In case i'm wrong about where the error is, the rest of my code is below.

    edit: removed full code. Thanks for looking!
    Last edited by zanyspydude; 10-22-2007 at 02:04 PM.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by zanyspydude View Post
    I need 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 have to free it.

    I don't see anything wrong with the way you use memory in the lookupPath() function, so the memory corruption must be happening somewhere else. Or, you are double-freeing a pointer, or freeing a bad pointer.

    EDIT: Actually I see the problem.

    Code:
    char* pName = (char*) malloc (strlen(name)+strlen(dirs[counter]+strlen("/")+1));
    Your parentheses are not in the right places. You want:

    Code:
    char* pName = (char*) malloc (strlen(name)+strlen(dirs[counter])+strlen("/")+1);

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    7
    thank you SOOOO much for your help. I never would have seen it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM