For example:
void func1(){
int i = 123;
func2(&i);
}
void func2(int *a){
*a = 456;
}
When func1
calling func2
, a pointer to local variable is passed to func2
-- the pointer is pointed to the stack. Is this safe for the rules of C?
Thanks.
What you are doing is safe as the local variable is still valid and within the scope. Accessing the local varaible outside of its scope is undefined behvaior but this is totally fine
In your case, you can safely use
&i
till the timei
is valid.Now, as we can see
i
has a lifetime till the end offunc1()
. As, thefunc2()
is being called from thefunc1()
and thefunc1()
has not yet finished execution, so,i
is still valid.That's why , usually passing the address of a local variable to another function is usually allowed (the variable's lifetime is not over) but,
return
ing the address of a local variable (immediately afterreturn
, local variables of the function cease to exist) is not allowed.TL;DR :You can safely use
&i
as the argument offunc2()
as shown here.Yes it is safe to pass a pointer to a local variable but you can't return a pointer to an automatic local variable from a function.
The scope of
i
isfunc1
and it outlives the call tofunc2
. So it is perfectly safe.As stated in most of the answers before, it is perfectly safe to pass the pointer to
func2()
in your special case.In a real world piece of software however, I consider this harmful as you don't have control on what
func2()
is doing with your variable.func2()
may create an alias to its parameter to use it asynchronously at a later point in time. And at that time, the local variableint i
may be gone when this alias is used later.So from my point of view passing a pointer to a local (automatic) variable is extremely dangerous and should be avoided.
You may do so if you declare the variable in
func1()
asstatic int i;
In that case, it is ensured, that the memory for
i
won't get recycled and overwritten. However you will need to setup some Mutex locking for access control to this memory in a concurrent environment.To illustrate this problem here is some code I just stumbled in yesterday while doing software testing at my customer. And yes, it crashes...
In this case, the data in the
DataBuff
is long gone when the pointer inASyncQueue.CommandArray[ASyncQueue.NextFreeEntry].BufferPtr
is used to store the data to the EEPROM.To fix this code, it is at least necessary to declare
static UINT8 DataBuff[25];
Additionally, it shall be considered to also declarestatic valueObj_t NVMemObj
as we don't know what the called function is doing with that pointer.To put it briefly: TL;DR
Even though it is legal in the C-language I consider it as harmful to pass pointers to automatic variables in a function call. You never know (and often you don't want to know) what exactly the called function does with the passed values. When the called function establishes an alias, you get in big trouble.
Just my 2 cents.
Yes, your code is safe.
As long as the object's lifetime is not over, it's safe to pass local variables like you do.