Should i enforce realloc check if the new block size is smaller than the initial ?
Posted
by nomemory
on Stack Overflow
See other posts from Stack Overflow
or by nomemory
Published on 2010-03-29T20:54:32Z
Indexed on
2010/03/29
21:03 UTC
Read the original article
Hit count: 299
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;
© Stack Overflow or respective owner