CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
What is the purpose of realloc( )?
Category C Interview Questions
Rating (4.1) By 23 users
Added on 10/22/2004
Views 11520
Rate it!
Answers:

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
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( )
may create a new region and all the old data are moved to the new region.




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.

I think the following program will help in understanding the point.

#include<stdio.h>
int main()
{
int *ptr,*ptr_new;
int num,newsize,i;

printf("
Enter the number of elements in the array");
scanf("%d",&num);
ptr= (int *)malloc( num * sizeof(int) );
if (ptr == NULL)
{
printf("
Cannot allocate memory");
return -1;
}
for(i=0;i<num;i++)
scanf("%d",&ptr[i]);

printf("
The elements entered are ::

");
for(i=0;i<num;i++)
printf("
Address of %d ----> %u",ptr[i],&ptr[i]);

printf("
Enter the new size of the array");
scanf("%d",&newsize);

ptr_new= (int *)realloc(ptr,newsize * sizeof(int));
if (ptr_new == NULL)
{
printf("
Cannot Re-allocate memory");
return -1;
}
for(i=0;i<newsize;i++)
{
if(i >= num )
{
printf("Enter %d element ",i+1);
scanf("%d",&ptr[i]);
}
}

for(i=0;i<newsize;i++)
printf("
Address of %d ----> %u",ptr_new[i],&ptr_new[i]);

system("pause");
return 0;
}



 Posted by: Syed Baseer Ahmed    

Contact Syed Baseer Ahmed  Contact Syed Baseer Ahmed

realloc is to resize the data bt due to tis the data ill corrupt so better dnt use tis one



 Posted by: what is realloc?    

Contact what is realloc?  Contact what is realloc?


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
Difference between arrays and pointers?
View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/961/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.67