Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   realloc doesnt re -allocate memory specified??? (http://www.go4expert.com/forums/realloc-doesnt-allocate-memory-specified-t25439/)

shakisparki 5Apr2011 15:00

realloc doesnt re -allocate memory specified???
 
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]);}


}


shabbir 5Apr2011 15:14

Re: realloc doesnt re -allocate memory specified???
 
What is the number of bytes you are specifying and how many bytes you think it has allocated?

ASRRAJ 6Apr2011 11:14

Re: realloc doesnt re -allocate memory specified???
 
memoPtr = (int*) realloc(memoPtr , (size / 2) * sizeof(int));
check this statment ...do you really need this size/2

xpi0t0s 6Apr2011 16:31

Re: realloc doesnt re -allocate memory specified???
 
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.


All times are GMT +5.5. The time now is 15:34.