GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 14 of 203    Print  
Difference between realloc() and free()?
 
The free subroutine frees a block of memory previously allocated by the malloc subroutine. Undefined results occur if the Pointer parameter is not a valid pointer. If the Pointer parameter is a null value, no action will occur.
The realloc subroutine changes the size of the block of memory pointed to by the Pointer parameter to the number of bytes specified by the Size parameter and returns a new pointer to the block. The pointer specified by the Pointer parameter must have been created with the malloc, calloc, or realloc subroutines and not been deallocated with the free or realloc subroutines. Undefined results occur if the Pointer parameter is not a valid pointer
 



  
Total Answers and Comments: 7 Last Update: June 13, 2007   
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: venkatesh
 

Realloc() is used to reallocate the memory for variable

Free() is used to free the allocated memory of a variable.

venkatesh_ch32@yahoo.com



Above answer was rated as good by the following members:
gr3go
September 22, 2005 08:28:41   #1  
sangappa        

RE: Difference between realloc() and free()...

realloc function takes 2 parameters.

example:

tpedef struct test

{

int i;

float j;

};

test *ptest (test*)malloc(sizeof(test));

realloc(ptest sizeof(test));

After realloc ptest contains new address and memory allocated will double the size of prvious memory.


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
April 13, 2006 08:45:16   #2  
venkatesh Member Since: November 2005   Contribution: 19    

RE: Difference between realloc() and free()...

Realloc() is used to reallocate the memory for variable

Free() is used to free the allocated memory of a variable.

venkatesh_ch32@yahoo.com


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
March 28, 2007 05:52:39   #3  
Raju Tewari        

RE: Difference between realloc() and free()...
Realloc () function used to expands the existing allocated memory if it finds enough continus memory
location that are required if enough continus memory are not available new memory of block is allocated.The existing data is copied and the origibnal memory block is freed. for example::

int *p new int[5];
p[1] 3;
p (int*) realloc (p sizeof(int) *5);
cout<<p[1];

 
Is this answer useful? Yes | No
April 24, 2007 03:47:54   #4  
kazam0076 Member Since: April 2007   Contribution: 1    

RE: Difference between realloc() and free()...
Hi Sangappa

I think the example mentioned in your answer wont double the size of memory pointed by the ptest. Instead it creates equal sized one and copies all the content to new memory. To get the expected result i believe the code should look like this.


>> realloc(ptest sizeof(test)*2); // to double the size of the mem.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
April 27, 2007 00:18:47   #5  
mitra.s Member Since: April 2007   Contribution: 1    

RE: Difference between realloc() and free()...

free() deallocates memoru allocated by maaloc calloc or realloc. Whenevr there is a need to allocate extra memory we can use realloc.
realloc means free+malloc once more. following example may explain this behaviour.

void main()
{
char *ptr;
ptr (char*)malloc(7);
strcpy(ptr "STRING");
cout<<"Content is "<<ptr;

realloc(ptr 10);
cout<<"nContent after Realloc is "<<ptr;
}

output
Content is STRING
Content after Realloc is //some garbage-> STRING is lost


 
Is this answer useful? Yes | No
May 27, 2007 00:29:00   #6  
Roopesh Majeti        

RE: Difference between realloc() and free()...
Mitra
as you mentioned that realloc will delete the existing memory and allocate the new memory . I tried it on solaris and following is found :

1. If the new size allocated is less than the existing memory size then realloc will allocate existing memory size. In other words will leave it.
2. If the new size allocated is more than the existing memory size then realloc will allocate new memory size.

In either of these two cases the realloc will not delete the contents.

What you said is partially correct. Realloc when trying to allocate required memory size and if it could not find a continuous memory then it will allocate new memory which will automatically give garbage as it now points to new memory.


Hope this clarifies many things.

-Roopesh.

 
Is this answer useful? Yes | No
June 13, 2007 02:15:13   #7  
SomGollakota Member Since: June 2007   Contribution: 48    

RE: Difference between realloc() and free()...

free() - releases the memory of the pointer passed as parameter to the OS/application consumption. Using the pointer after free() will result in undefinded results

realloc() - used to resize the memory held by the pointer to the number of bytes specificed. If the new size is larger than current size new memory is allocated. If it is less the remaining (additional) bytes are released to general OS/application consumption.


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape