S
subramanian100in
In the following link,
http://www.c-faq.com/malloc/retaggr.html
The following ANSWER is given to a question(comp.lang.c FAQ list ·
Question 7.5a) :
Whenever a function returns a pointer, make sure that the pointed-to
memory is properly allocated. For example, make sure you have not done
something like
#include <stdio.h>
char *itoa(int n)
{
char retbuf[20]; /* WRONG */
sprintf(retbuf, "%d", n);
return retbuf; /* WRONG */
}
When a function returns, its automatic, local variables are discarded,
so the returned pointer in this case is invalid (it points to an array
that no longer exists).
One fix would be to declare the return buffer as
static char retbuf[20];
This fix is imperfect, since a function using static data is not
reentrant.
My question is what "reentrant" means here? Why is static data inside
function discouraged ?
http://www.c-faq.com/malloc/retaggr.html
The following ANSWER is given to a question(comp.lang.c FAQ list ·
Question 7.5a) :
Whenever a function returns a pointer, make sure that the pointed-to
memory is properly allocated. For example, make sure you have not done
something like
#include <stdio.h>
char *itoa(int n)
{
char retbuf[20]; /* WRONG */
sprintf(retbuf, "%d", n);
return retbuf; /* WRONG */
}
When a function returns, its automatic, local variables are discarded,
so the returned pointer in this case is invalid (it points to an array
that no longer exists).
One fix would be to declare the return buffer as
static char retbuf[20];
This fix is imperfect, since a function using static data is not
reentrant.
My question is what "reentrant" means here? Why is static data inside
function discouraged ?