INTERVIEW QUESTIONS
C
DYNAMIC MEMORY ALLOCATION IN C
DETAILS
Question: What is the purpose of realloc( )?
Answer: The function realloc(ptr,n) uses two arguments.the first argument ptr is a pointer to a block of memory for which the size is to be altered.The second argument n specifies the<br>new size.The size may be increased or decreased.If n is greater than the old size and if sufficient space is not available subsequent to the old region, the function realloc( )<br>may create a new region and all the old data are moved to the new region.
|
Question:
What is the purpose of realloc( )?
Answer:
The function realloc(ptr,n) uses two arguments.the first argument ptr is a pointer to a block of memory for which the size is to be altered.The second argument n specifies the<br>new size.The size may be increased or decreased.If n is greater than the old size and if sufficient space is not available subsequent to the old region, the function realloc( )<br>may create a new region and all the old data are moved to the new region. Source: CoolInterview.com
<br>realloc() can be used for re-sizing the allocated memory. No doubt in it. But due to this, some data which is there in that memory may get corrupted. So it is better not to use the realloc function. <br><br>I think the following program will help in understanding the point.<br><br>#include<stdio.h><br>int main()<br>{<br> int *ptr,*ptr_new;<br> int num,newsize,i;<br> <br> printf("<br>Enter the number of elements in the array");<br> scanf("%d",&num);<br> ptr= (int *)malloc( num * sizeof(int) );<br> if (ptr == NULL)<br> {<br> printf("<br>Cannot allocate memory");<br> return -1; <br> }<br> for(i=0;i<num;i++)<br> scanf("%d",&ptr[i]);<br> <br> printf("<br> The elements entered are ::<br><br>");<br> for(i=0;i<num;i++)<br> printf("<br>Address of %d ----> %u",ptr[i],&ptr[i]);<br> <br> printf("<br>Enter the new size of the array");<br> scanf("%d",&newsize);<br> <br> ptr_new= (int *)realloc(ptr,newsize * sizeof(int));<br> if (ptr_new == NULL)<br> {<br> printf("<br>Cannot Re-allocate memory");<br> return -1; <br> }<br> for(i=0;i<newsize;i++)<br> {<br> if(i >= num )<br> {<br> printf("Enter %d element ",i+1);<br> scanf("%d",&ptr[i]);<br> } <br> }<br> <br> for(i=0;i<newsize;i++)<br> printf("<br>Address of %d ----> %u",ptr_new[i],&ptr_new[i]);<br><br> system("pause");<br> return 0;<br>}<br> Source: CoolInterview.com
Answered by: Syed Baseer Ahmed | Date: 12/5/2007
| Contact Syed Baseer Ahmed
realloc is to resize the data bt due to tis the data ill corrupt so better dnt use tis one Source: CoolInterview.com
Answered by: what is realloc? | Date: 7/11/2009
| Contact what is realloc?
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.
|