3

Consider the following C program

#include <stdio.h>

typedef struct s {
  int x;
} s_t;

int main() {
  int x;
  s_t a;

  scanf("%d", &x);

  if (x > 0) {
    s_t a;
    a.x = x;
  }

  printf("%d\n", a.x);
}

The a struct variable in the if branch clearly shadows the a struct variable in main. One would expect that the output in printf would be undefined, but with GCC the scoped variable seems to equal the main variable.

For example

gcc test.c -o test
echo 10 | ./test

will output 10.

On the other hand, running this through clang, does as expected

clang test.c -o test
echo 10 | ./test

outputs -2145248048.

Is this a GCC bug or is there some sort of undefined behaviour that this is triggering?

gcc 4.8.2 clang 3.4

2
  • 3
    a at main is uninitialize. no sense. try s_t a = {77}; at top level in main
    – BLUEPIXY
    Jun 15, 2015 at 20:59
  • 1
    You could improve your test by (a) printing a.x in the scope of the if statement, and (b) initializing a.x in the top-level scope of main(), and (c) printing a.x before the if statement as well as after. These steps should eliminate undefined behaviour and make it clearer what's going on. Then we can start discussing what you see and whether there's a bug. But while you have undefined behaviour in your code, you have no recourse for complaint; anything can happen and it is a valid result of invoking undefined behaviour. Jun 15, 2015 at 21:05

4 Answers 4

9

As others mentioned, you're reading an uninitialized local variable and that's undefined. So, anything is legit. Having said that, there is a particular reason for this behavior: gcc reuses variables on the stack as much as it can (i.e., as long as the generated code is provably correct). You can fine-tune using the -fstack-reuse option.

To disable stack reuse:

gcc test.c -o test -fstack-reuse=none
echo 10 | ./test
4195808 # prints some random number.

To enable stack reuse for all variables:

gcc test.c -o test -fstack-reuse=all  #, or -fstack-reuse=named_vars
echo 10 | ./test
10 # prints 10, as it reuses the space on the stack.

This is fully documented on GCC Code Generation Options.

3
  • Interesting. Are you able to show this in use, with different results. If so, it would be worth adding to your answer. Jun 15, 2015 at 21:08
  • 1
    @haccks, added the reference. Jun 15, 2015 at 21:22
  • Interesting from an academic viewpoint. However, that might tempt some to exploit that instead of writing correct code. Jun 15, 2015 at 22:10
4

One would expect that the output in printf would be undefined

It is undefined. Undefined behaviour means that anything can happen, including (but not limited to) any particular output.

In this case what's likely happening is that the compiler optimizes both a to have the same address.

1
  • If you inspect the assembly generated (e.g. by using -S switch of gcc) it would probably show exactly what is happening
    – M.M
    Jun 15, 2015 at 21:02
4

No compiler bug, your program invokes undefined behavior.

You are reading an uninitialized automatic object and C says it has indeterminate value and that reading it is undefined behavior.

Give a.x (the one declared in main scope) a value to give your program a specified behavior.

3

Is this a GCC bug or is there some sort of undefined behavior that this is triggering?

It's undefined behavior. s_t a; in the if statement hides the previous declaration of a.
In the block

if (x > 0) {
    s_t a;
    a.x = x;
}  

x is assigned to the local a.x. After this block, a is no longer available and in printf you are accessing an uninitialized variable. Uninitialized variables may invoke UB.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.