Should i enforce realloc check if the new block size is smaller than the initial ?
- by nomemory
Can realloc fail in this case ?
int *a = NULL;
a = calloc(100, sizeof(*a));
printf("1.ptr: %d \n", a);
a = realloc(a, 50 * sizeof(*a));
printf("2.ptr: %d \n", a);
if(a == NULL){
printf("Is it possible?");
}
return (0);
}
The output in my case is:
1.ptr: 4072560
2.ptr: 4072560
So 'a' points to the same adress.
So should i enforce realloc check ?
Later edit:
Using MinGW compiler under Windows XP.
Is the behaviour similar with gcc on Linux ?
Later edit 2:
Is it OK to check this way ?
int *a = NULL, *b = NULL;
a = calloc(100, sizeof(*a));
b = realloc(a, 50 * sizeof(*a));
if(b == NULL){
return a;
}
a = b;
return a;