This may be a newbie question, but is there a way in C / C ++ to prevent a function from accepting a pointer to a local variable?
Consider this code:
int* fun(void)
{
int a;
return &a;
}
The compiler generates a warning that the pointer cannot be returned. Now consider the following:
int* g;
void save(int* a)
{
g = a;
}
void bad(void)
{
int a;
save(&a);
}
This will go through the compiler without warning, which is bad. Is there any attribute or something to prevent this from happening? That is, something like:
void save(int __this_pointer_must_not_be_local__ * a)
{
g = a;
}
Thanks in advance if anyone knows the answer.
haael source
share