G***n 发帖数: 877 | 1 Hi,
I have a question using C. I want to
allocate the memory using realloc() or
malloc() inside a function fun(), the
return of this function is the pointer
pointing to the allocated memory. There
is no free() inside fun(), because I
need to grasp the allocated memory in
fun() by its return.
In the main function, I define char
*pStr= fun() to call this function and
then try to free pStr later using
free(pStr). The program runs forever at
free(pStr) there. However, if I do not
use free() function to release the
memory, the code can pass the test.
That mean I cannot use free() to free
pStr returned by a function. Then my
question is how to release the memory
space allocated inside fun()? If I
don't use free(), the code pass the
test, however, I think the memory
without release would be wrong once the
memory is full.
Does anyone have any idea of what I am
wrong here? |
|