MEMORY ALLOCATION - realloc()

Published: 16 April 2020
on channel: Solution Manual
10
0

int main()
{
int i,n,*p;
//Allocate the memory based on user given size

printf("\n Enter the Number of Elements\n");
scanf("%d",&n);
p=(int*)malloc(n*sizeof(int)); // if elements is equal to 2 then 2x4(size of int)=8 bytes will be allocated

// PRINT THE MEMORY LOCATION
for(i=0;i lt n;i++)
{
printf("\nMemory Location of %d is :::: %d",i,&p[i]);
}
// GET THE ELEMENTS FROM USER ON MEMORY LOCATIONS

for(i=0;i lt n;i++)
{
printf("\n Enter the Element %d \t ",i);
scanf("%d",&*(p+i));
}

//PRINT THE ELEMENTS
printf("\n ::::Elements::::\n");

for(i=0;i lt n;i++)
{
printf("\n%d\n",*(p+i));
}

// REALLOCATION STARTS HERE
// COPY PASETE THE PREVIOUS SET OF CODE HERE

printf("\n Enter the Number of Elements for REALLOCATION\n");
scanf("%d",&n);
p=realloc(p,n*sizeof(int)); // change the allocation size here using realloc() function

// PRINT THE MEMORY LOCATION
for(i=0;i lt n;i++)
{
printf("\nReallocated Memory Location of %d is :::: %d",i,&p[i]);
}
// GET THE ELEMENTS FROM USER ON MEMORY LOCATIONS

for(i=0;i lt n;i++)
{
printf("\n Enter the New Element %d \t ",i);
scanf("%d",&*(p+i));
}

//PRINT THE ELEMENTS
printf("\n ::::REALLOCATED Elements::::\n");

for(i=0;i lt n;i++)
{
printf("\n%d\n",*(p+i));
}
return 0;
}


On this page of the site you can watch the video online MEMORY ALLOCATION - realloc() with a duration of hours minute second in good quality, which was uploaded by the user Solution Manual 16 April 2020, share the link with friends and acquaintances, this video has already been watched 10 times on youtube and it was liked by 0 viewers. Enjoy your viewing!