...
In this noncompliant code sample, the address of local variable c_str
is assigned to the variable p
, which has file scopestatic storage duration. The assignment itself is legalvalid, but it is illegal invalid for c_str
to go out of scope while p
holds its address, as happens at the end of dont_do_this
()
.
...
If it is necessary for p
to be defined with file scope but with static storage duration but c_str
with a more limited scopeduration, then p
can be set to NULL
before c_str
is destroyed. This practice prevents p
from taking on an indeterminate value, although any references to p
must check for NULL
.
...
In this noncompliant code sample, the function squirrel_away()
stores a pointer to local stack variable local
into a location pointed to by function parameter ptr_param
. Since Because it can be assumed that the pointer variable to which ptr_param
points remains alive upon squirrel_away()
's return, it is illegal is invalid for local
to go out of scope.
...
Compliant Solution
The variable local
does not go out of scope for the entire program has static storage duration, so ptr
is live and valid in the function rodent()
:
...