-3

The following snippet is intentionally accessing the next sizeof(int) bytes following t[4], so I am aware of the mistake that is being made here. I am just doing this as an experiment to see how the compiler handles the stack allocations.

int t[5], i;

for (i = 0; i <= 5; i++) {
   t[i] = 0;
}

When executing this code on Windows, using a ported version of the GNU C Compiler, the program always gets stuck in an infinite loop. I am sure that this could only happen because t and i are allocated sequentially on the stack one after the other and t[5] points to to the same address as the i variable. Therefore, when executing t[5] = 0 the program actually sets the value of i to zero.

However, when compiling this with a different version of the GNU C Compiler, I never get the infinite loop. The address of t[5] is not the same as the address of i.

My question is, why this different behavior? I know you should not assume anything about the outcome of this, but is it not the case that stack allocations should happen in the same way?

What I am really curious about is how does the compiler manage those stack allocations. Is there any padding? Is the order always the same as in the source code? Obviously this has nothing to do with the C standard and there are differences between implementations, or even different versions of the same compiler. I am curious though what are the possible outcomes and considerations in this particular case.

17
  • 3
    There is a difference between C and C++. Please do not tag the question with both. Feb 25, 2016 at 8:55
  • 9
    "I think that the the stack allocations should happen in the same way." - why do you think this? This is entirely an implementation detail, not in any way defined by the language.
    – BoBTFish
    Feb 25, 2016 at 8:55
  • 3
    Why do you think the cause of the endless loop is the assignment modifying i? It can just as well be the compiler knowing that i <= 5 can never be true and optimizing the comparison out. Feb 25, 2016 at 9:01
  • 1
    You're using different compilers on different platforms, thus the different behaviour
    – Shark
    Feb 25, 2016 at 9:02
  • 5
    Undefined behavior = anything can happen. You can tell that anything does indeed happen in both cases, so the behavior is as expected (there is no expected behavior). Investigating why undefined behavior caused a particular result on a particular system, at a particular point in time, is simply not meaningful practice.
    – Lundin
    Feb 25, 2016 at 9:14

3 Answers 3

9

You are dealing with undefined behaviour. The compiler isn't required to lay out automatic variables sequentially (as they appear in the source code). Some of them might be in registers or they might be ordered in a different way, if, for example, smaller offsets are cheaper.

Such a requirement exists only for the members of a struct (with the members having arbitrary padding in between).

Is there any padding?

Yes, the compiler would honour the alignment requirements of each type and place the variables accordingly.

Is the order always the same as in the source code?

No, but this is the thing many exploits rely on. A buffer overflow may overwrite an adjacent variable and compromise the execution of the entire program.

4

Another people said why you are getting this behaviour from standard point of view, I will say what potentially could happen after your code compiled optimised and executed.

First : loop might be unrolled and executed 6 times:

t[0] = 0;
t[1] = 0;
t[2] = 0;
t[3] = 0;
t[4] = 0;
t[5] = 0;
i = 6;

It is allowed optimisation and that is what could happen. More: if i is not used later it can be removed altogether.

Second : Compiler might keep i in register without doing any stack allocation.

Third : It might place variable in any order on stack. There is no actual requirements on order in which varaibles are kept in memory (and on their locality too).

How to know what happen? Look at the generated assembly. It is the only way to know what happens.

BTW: Infinite loop does not always happens on Windows. In fact I weren't able to force your code verbatim to be an infinite loop.

1
  • thanks! I didn't really consider compiler optimizations or the fact that the order in the stack is not necessarily the same. Feb 25, 2016 at 9:38
3

Accessing t[5] is an Undefined Behaviour. The last item is t[4] (t[0],t[1],t[2],t[3],t[4]) there is no t[5].

By Undefined Behaviour, anything may happen. It may gives expected results or totally messed up.

My question is, why this different behavior?

As I wrote before, it is UB you can not expect anything. Even you may get another result in the same machine if you try it many times.

7
  • @Garf365: The C++ standard can't explain it, but it is certainly not the case that nothing can. Feb 25, 2016 at 9:09
  • @BenjaminLindley Yes but why? why I should know what will happen if I make undefined thing? Feb 25, 2016 at 9:10
  • 2
    @HumamHelfawi: I don't know. Perhaps you are interested in how compilers work. Feb 25, 2016 at 9:12
  • @BenjaminLindley it is not about how the compiler works. it about how the compilers deal with the things that you should not use :) it should not be even been documented and you can not even raise a case in the court towards the compiler if the compiler change this behaviour in the next version of it :) Feb 25, 2016 at 9:13
  • 1
    @AlexandruPele perhaps the question is simply... badly worded. It seems to me that you're asking something in THIS direction, but applied to variables and "same code, different compilers, different platform" scope.
    – Shark
    Feb 25, 2016 at 9:26

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.