realloc

This is a discussion on realloc within the C Programming forums, part of the General Programming Boards category; Hi guys, I've been using the realloc() function for a while, and I think the function can sometime cause a ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    84

    realloc

    Hi guys,

    I've been using the realloc() function for a while, and I think the function can sometime cause a memory leak if not used well.
    Here is the code:
    Code:
    int main(void)
    {
    	long *pArray, *pTemp;
    	int i;
    
    	pArray=malloc(sizeof(*pArray)*5);
    	if (pArray==NULL)
    	{
    		puts("Failed to allocate memory!");
    		exit(1);
    	}
    
    	pTemp=realloc(pArray,sizeof(*pTemp)*1000);
    	if (pTemp==NULL)
    	{
    		puts("Failed");
    		exit(1);
    	}
    
    	for (i=0;i<10;++i)
    		pTemp[i]=i;
    
                    // ****************************************
                    // This is the new code line I've never used before!
                    // ****************************************
    	if (pTemp==pArray)
    	{
    		pArray=pTemp;
    		puts("Same address!");
                    }
    	else
    	{
    		free(pArray);
                                    pArray=pTemp;
    		puts("Diffetent Address");
                    }
    
    	free(pTemp);
    	return 0;
    }
    For some reason I never saw this implemention anywhere in my books or on the net.
    Because realloc priority is to reallocate memory in the preview allocation address (to skip copying the old values), this not always the case, so assigning the new allocation address (pTemp) to the old one (pArray) will cause a memory leak.
    Am I right?

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    957
    No, that's not correct. By definition realloc() will deallocate the old memory (unless it can't allocate enough new storage). What that means is that you absolutely should not free pArray the way you are in your example. It will have already been deallocated by realloc(). Your code is overly complex. You should use realloc() something like this:
    Code:
    #include <stdlib.h>
    #include <assert.h>
    
    int main(void)
    {
      void *p, *t;
    
      p = malloc(50);
      assert(p != NULL);
      t = realloc(p, 5000);
      if(t == NULL)
      {
        /* deal with insufficient memory condition.
         * note that p is still valid at this point.
         */
      }
      else
      {
        /* allocation was successful, reassign p */
        p = t;
      }
    
      /* do something with p */
    
      free(p);
    
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. did i understood right this explantion of realloc..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 10-24-2008, 07:26 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. using realloc
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 12-06-2005, 04:00 PM
  4. segfault on realloc
    By ziel in forum C Programming
    Replies: 5
    Last Post: 03-16-2003, 03:40 PM
  5. Realloc inappropriate for aligned blocks - Alternatives?
    By zeckensack in forum C Programming
    Replies: 2
    Last Post: 03-20-2002, 01:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21