Yttrill
Yttrill

Reputation: 4921

Suppress gcc 4.2.1 warning with pragma

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).

Upvotes: 1

Views: 844

Answers (2)

caf
caf

Reputation: 239041

This appears to make the warning go away for me:

void *get_stack(void) {
  void *v = &v;
  return v;
}

Upvotes: 2

Matthew Flaschen
Matthew Flaschen

Reputation: 284806

As the docs note, you can only control options that show up for -fdiagnostics-show-option. It does not show up for me. I'm running 4.4.1, but I doubt it would for 4.2.1 either.

You may want to file a bug to get it included in the diagnostic system.

Upvotes: 0

Related Questions