I would like to suppress a particular warning issued by gcc caused by returning the address of a local variable.
#include <stdio.h>
#pragma GCC diagnostic ignored "-Waddress"
void *get_stack() {
unsigned long v;
return &v;
}
int main()
{
void *p = get_stack();
printf("stack is %p\n",p);
return 0;
}
>gcc -fdiagnostics-show-option p.c
p.c: In function ‘get_stack’:
p.c:5: warning: function returns address of local variable
Platform: this issue exists at least on MacOSX 10.5 Snow Leopard,
I haven’t tried on Linux yet.
In case you’re wondering why: I would like to run with warnings turned into errors
to halt a long winded build process so I can actually SEE problems and be forced
to fix them.
This particular code isn’t a bug, it is a “portable” technique for finding a
the stack pointer (which works on MSVC too). [Actually it won’t work on the
Itanium which has two stack pointers]
The stack pointer is required for use by a garbage collection routine
(to search for pointers on the stacks of suspended threads).
This appears to make the warning go away for me: