M
Mark McIntyre
On Sun, 29 Jun 2003 03:16:07 +0200, in comp.lang.c , "Eitan
snipped
I've no idea what your problem is. realloc frees the pointer that was
malloced - yes, it often does, its supposed to, if there's not enough
memory at the original location. This is NOT a leak, its correctly
freed.
fine, though you do not need the cast. Or rather, if you do, you're
compiling C++ not C, and you should be using new not malloc.
This is a memory leak because if realloc fails, it returns NULL but
/without/ freeing the original pointer. You MUST use a temporary when
reallocing.
struct st_Test* tmp = realloc(ps_Test, iCounter+1);
if(!tmp)
// failed to find memory, handle the error
else
/// ok, proceed
too late, if the realloc failed, you leaked.
snipped
I've no idea what your problem is. realloc frees the pointer that was
malloced - yes, it often does, its supposed to, if there's not enough
memory at the original location. This is NOT a leak, its correctly
freed.
struct st_Test *pst_Test = NULL;
if(!pst_Test) /* not yet allocated */
pst_Test = (struct st_Test*)malloc(1 * sizeof(struct st_Test));
fine, though you do not need the cast. Or rather, if you do, you're
compiling C++ not C, and you should be using new not malloc.
else /* resize by 1 , Note: This is my problem!!!! */
pst_Test = (struct st_Test*)realloc(pst_Test,(iCounter+1)
*sizeof(struct st_Test));
This is a memory leak because if realloc fails, it returns NULL but
/without/ freeing the original pointer. You MUST use a temporary when
reallocing.
struct st_Test* tmp = realloc(ps_Test, iCounter+1);
if(!tmp)
// failed to find memory, handle the error
else
/// ok, proceed
// check that malloc\realloc worked
if(!pst_Test) return -1;
too late, if the realloc failed, you leaked.