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]);} }
memoPtr = (int*) realloc(memoPtr , (size / 2) * sizeof(int)); check this statment ...do you really need this size/2
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.