c - Preventing local pointers -
maybe it's newbie question, there method in c/c++ prevent function accepting pointer local variable?
consider code:
int* fun(void) { int a; return &a; }
the compiler generate warning pointer can not returned. consider this:
int* g; void save(int* a) { g = a; } void bad(void) { int a; save(&a); }
this pass through compiler without warning, bad. there attribute or prevent happening? i.e. like:
void save(int __this_pointer_must_not_be_local__ * a) { g = a; }
thanks in advance if knows answer.
no, there no reliable , portable way tell pointer local pointer heap object. there no way declaratively prevent this, either.
there hacks dependent on memory layout of particular system work @ runtime invoking unspecified behavior (see this answer example), on own if decide give them try.
Comments
Post a Comment