SPRUIG8J January 2018 – March 2024
The compiler, when invoked with optimization, assumes that if the address of a local variable is passed to a function, the function changes the local variable by writing through the pointer. This makes the local variable's address unavailable for use elsewhere after returning. For example, the called function cannot assign the local variable's address to a global variable or return the local variable's address.
If your code uses aliases in this way and uses optimization, you must use the --aliased_variables option. For example, suppose your code is similar to the following, in which the address of the local variable x is passed to the function f(), which aliases glob_ptr to that address and returns the address. If this example were to be compiled with optimization, the --aliased_variables option would be needed in order for the function f() to be able to successfully perform its actions.
int *glob_ptr;
g()
{
int x = 1;
int *p = f(&x);
*p = 5; /* p aliases x */
*glob_ptr = 10; /* glob_ptr aliases x */
h(x);
}
int *f(int *arg)
{
glob_ptr = arg;
return arg;
}