http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50476
Bug #: 50476
Summary: Warn of pointer set to object whose lifetime is
limited
Classification: Unclassified
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: enhancement
Priority: P3
Component: c
AssignedTo: [email protected]
ReportedBy: [email protected]
Consider the following code:
<code>
#include <stdio.h>
int *x = NULL;
void f(void)
{
int y = 1;
x = &y;
}
int main(void)
{
f();
printf("int: %d\n", *x);
return 0;
}
</code>
Function f() assigns a global pointer to a local object, so that the global
pointer refers to the local object's address even when the object's lifetime
ends. This represents undefined behaviour, and therefore can be a potential
source of problems. It would be great if gcc at least threw a warning
informing the user of this problem, similar to how Bug 14156 handles it's use
case.