Question:
Difference between realloc() and free()?
Answer:
Realloc function takes 2 parameters. <br><br>example:<br><br>tpedef struct test<br><br>{<br><br>int i;<br><br>float j;<br><br>};<br><br>test *ptest = (test*)malloc(sizeof(test));<br><br>realloc(ptest,sizeof(test));<br><br>After realloc ptest contains new address and memory allocated will double the size of prvious memory.<br> Source: CoolInterview.com
realloc() is function allocated dynamically. we can modify the memory in terms of reducing or increasing memory for variables when it required then we can decrease or increase more than our prespecified memory, the free() function frees the previously allocate memory Source: CoolInterview.com
Answered by: nagaraju | Date:
| Contact nagaraju
realloc()is used to allocate memory dynamicaaly<br>where as free()is used to deallocate unused memory Source: CoolInterview.com
Answered by: sreelatha | Date:
| Contact sreelatha
Realloc function re-allocates previously allocated memory as per our requirement. we have to pass the pointer as first parameter, the size of new memory. Realloc has take care about the allocation, the data in pointer should not be changed and the location is changed depending on the memory size. It is pure 'C' native function.<br><br>free() function used to deallocate memory which allocated with malloc or calloc functions. Source: CoolInterview.com
Answered by: Ravi Shanklar | Date: 12/10/2007
| Contact Ravi Shanklar
The realloc() function changes the size of the block pointed to by ptr to size bytes and returns a pointer to the (possibly moved) block. The contents will be unchanged up to the lesser of the new and old sizes. If the new size of the block requires movement of the block, the space for the previous instantiation of the block is freed. If the new size is larger, the contents of the newly allocated portion of the block are unspecified. If ptr is NULL, realloc() behaves like malloc() for the specified size. If size is 0 and ptr is not a null pointer, the space pointed to is freed.<br> Source: CoolInterview.com
Answered by: riya | Date: 12/26/2007
| Contact riya
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|