65

I have this question, which i thought about earlier, but figured it's not trivial to answer

int x = x + 1;
int main() {
  return x;
}

My question is whether the behavior of the program is defined or undefined if it's valid at all. If it's defined, is the value of x known in main?

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • Interesting. At least GCC gives 1, even with `-std=c++98 -pedantic`. – Philipp Jul 22 '10 at 13:00
  • Compiling this with MSVC9 (15.00.30729.01) gives 1. – akira Jul 22 '10 at 13:14
  • Sequence Point comes to mind http://en.wikipedia.org/wiki/Sequence_point –  Jul 22 '10 at 13:39
  • Why would someone write code like this? If something, this might/will confuse the static analyzer you're using. I would consider the behaviour undefined, even though many compilers give consistent result x=1. – Schedler Jul 22 '10 at 14:09
  • 9
    @Schedler i recommend against such code. It's purely a quiz, without any practical background on my part. :) – Johannes Schaub - litb Jul 22 '10 at 14:15
  • For the [constexpr case](http://stackoverflow.com/q/34276373/1708801) it was recently clarified that this is not well defined. It seems to imply that this case is but the changed language still seems vague on this point. I don't know how I missed this question when I was doing my research previously. – Shafik Yaghmour Dec 21 '15 at 00:51
  • @shafik. I would disagree and it seems your answer does aswell. It seems well defined, but illformed. – Johannes Schaub - litb Dec 21 '15 at 10:25
  • Well this case is not *constant initialization* which is what the defect report is about. I missed this phrase *Static initialization shall be performed before any dynamic initialization takes place*. Which should make this well-formed. I don't know why, when I originally read the changes I thought it was unclear for this case. – Shafik Yaghmour Dec 21 '15 at 12:16

4 Answers4

102

I'm pretty sure it's defined, and x should have the value 1. §3.6.2/1 says: "Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place."

After that, I think it's all pretty straightforward.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
11

My question is whether the behavior of the program is defined or undefined if it's valid at all. If it's defined, is the value of x known in main?

This code is definitely not clean, but to me it should work predictably.

int x puts the variable into the data segment which is defined to be zero at the program start. Before main(), static initializers are called. For x that is the code x = x + 1. x = 0 + 1 = 1. Thus the main() would return 1.

The code would definitely work in unpredictable fashion if x is a local variable, allocated on stack. State of stack, unlike the data segment, is pretty much guaranteed to contain undefined garbage.

Dummy00001
  • 16,630
  • 5
  • 41
  • 63
6

The 'x' variable in stored in the .bss, which is filled with 0s when you load the program. Consequently, the value of 'x' is 0 when the program gets loaded in memory.

Then before main is called, "x = x + 1" is executed.

I don't know if it's valid or not, but the behavior is not undefined.

Tomaka17
  • 4,832
  • 5
  • 29
  • 38
0

Before the main call x must be initialized to 0 therefore it's value must be 1 one you enter main, and you will return 1. It's a defined behavior.

LostMohican
  • 3,082
  • 7
  • 29
  • 38