realloc doesnt re -allocate memory specified???

shakisparki's Avatar, Join Date: Mar 2011
Newbie Member
please why doesnt realloc give the exact amount of bytes i specified??

Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
   int size ,counter = 0 ;
   int *memoPtr ;
   int  *newMemoPtr;
   printf("Enter Size of Array > "); scanf("%d" , &size);

   memoPtr = (int *)calloc( size, sizeof(int));

   if (memoPtr != NULL){

       while (counter < size)
       {
           scanf("%d" , &memoPtr[counter++]);
       }
   }
   else{
    printf("Memory couldnt be allocated");  }

    for (counter = 0; counter < size ; counter++){
        printf("%d , " , memoPtr[counter]);}

    memoPtr = (int*) realloc(memoPtr , (size / 2) * sizeof(int));
    printf("\n");

    for (counter = 0; counter < size ; counter++){
        printf("%d , " , memoPtr[counter]);}


}

Last edited by shabbir; 5Apr2011 at 15:13.. Reason: Code blocks
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
What is the number of bytes you are specifying and how many bytes you think it has allocated?
0
ASRRAJ's Avatar, Join Date: Jun 2008
Light Poster
memoPtr = (int*) realloc(memoPtr , (size / 2) * sizeof(int));
check this statment ...do you really need this size/2
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Don't be a smartass. YOU are asking US for help, therefore you don't know what you're doing, and we do. So if we ask questions, answer them even if you think they're irrelevant.

Have another look at the loop at the end of the program.