5

I have the following test code, file test.c:

#include <stdio.h>

int *func()
{
    int i = 123;
    return &i;
}

int main()
{
    printf("%d\n", *func());
}

If I use the command to compile it that is OK,

gcc test.c -o test

It will have the following warning information:

warning: address of stack memory associated with local variable 'i'
  returned [-Wreturn-stack-address]
return &i;
        ^
1 warning generated.

But it can output the result: 123

If I use the command:

gcc -Werror test.c -o test

It will have the following error information:

error: address of stack memory associated with local variable 'i'
  returned [-Werror,-Wreturn-stack-address]
return &i;
        ^
1 error generated.

Now I want to use the -Werror option, but I also want to ignore the address of stack memory associated with local variable 'i' warning. What should I do?

17
  • 1
    You realize that returning a pointer to stack variables is a terrible idea, right? If you make any function calls at all (including implicit function calls, sometimes performed when you initialize a struct/array with = {0}; for instance), the value referenced by the pointer will no longer be valid. This is a warning for a reason. Commented Aug 9, 2016 at 1:20
  • 1
    Thanks @ShadowRanger I got it, I only want to resolve it temporary. Commented Aug 9, 2016 at 1:28
  • Some canonicals for the error you can not ignore - as said in an answer (what is the point of getting rid of a warning if the result is almost certainly a crash at runtime?): Commented Oct 22, 2022 at 5:14
  • Same error message: Using C-string gives Warning: "Address of stack memory associated with local variable returned" Commented Oct 22, 2022 at 5:20
  • C++ Returning reference to local variable (2011, five answers and 137 upvotes). Commented Oct 22, 2022 at 5:20

2 Answers 2

12

Most gcc warnings can be disabled by prefixing the name of the warning with no-, e.g. -Wno-return-stack-address.

That said, this is not something you want to ignore; returning pointers to stack variables is undefined behavior, and while it has semi-predictable results on most compilers, it's incredibly fragile; any function call at all, implicit or explicit, could stomp on the value that pointer is referencing.

Sign up to request clarification or add additional context in comments.

8 Comments

In a newer version of GCC it would be -Wno-return-local-addr instead of -Wno-return-stack-address
What is the latest version of GCC to support -Wno-return-stack-address? (I can't find it anywhere in the documentation for the current version). That would put version constraints on what the OP was using. Search engines are very unwilling to reveal this information. Is there a more efficient technique when it comes to navigating the GCC documentation, incl. older versions?
@PeterMortensen: No idea. The OP told me what the warning switch was, I told them to add no- to the name to disable it.
A search should be without the three letters "-no" (or use just the last part). -Wreturn-local-addr was apparently introduced with GCC 4.8 (was released 2015-06-23). It wasn't in GCC 4.7.
|
0

According to the gcc documentation about -Werror, you can "negate -Werror for specific warnings" with -Wno-error=. In our case, we can ignore -Wreturn-stack-address -Wno-error=return-stack-address as follows:

gcc -Werror -Wno-error=return-stack-address test.c -o test

As already stated another answer here, you can disable most warnings by appending no- to the warning name. In the documentation linked above:

Each of these specific warning options also has a negative form beginning -Wno- to turn off warnings; for example, -Wno-implicit."

I recognize that the example given is this question is just an example. Places where I've needed this are from compiling external libraries that I cannot edit, due to licensing or other reasons. Often, I can put in a ticket to remove the warning, but in the meantime, I assess the risk and choose to silence the warning until an update comes.

1 Comment

I think it is Clang in disguise, not GCC (the executable "gcc" is aliased to the Clang compiler (on a Mac)). " -Wreturn-stack-address" is a signature of Clang (I don't think it exists in GCC).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.