Jul 19, 2011 at 2:37am Jul 19, 2011 at 2:37am UTC
int static errx = (CorP == CALL ? 0 : errx);
This is undefined behavior. If CorP != CALL
, then it's the same thing as saying static int x = x;
. It will compile and run but you don't know what it will do. What should the compiler do in this case? Leave x undefined maybe, but you can't guarantee that. Your second case does not exhibit any undefined behavior, hence it works as you expect.
Last edited on Jul 19, 2011 at 2:37am Jul 19, 2011 at 2:37am UTC
Jul 19, 2011 at 2:43am Jul 19, 2011 at 2:43am UTC
The condition is ALWAYS defined, either true or false. CALL is a constant, CorP is a function argument.
So, why int static errx = errx;
also when (CorP == CALL)
is true?
Jul 19, 2011 at 2:46am Jul 19, 2011 at 2:46am UTC
yes, it's like saying;
int x = (a == b ? 0 : x);
x is already unknown, not initialized, so if the condition is False, x cannot be equal to x -- at the first run x has garbage value.
even it works magically, the logic is wrong.
but if you do;
1 2
int static errx = 1; //a default value
errx = (CorP == CALL ? 0 : errx);
will work correctly.
Last edited on Jul 19, 2011 at 2:51am Jul 19, 2011 at 2:51am UTC
Jul 19, 2011 at 3:11am Jul 19, 2011 at 3:11am UTC
Remember that static variables are initialized exactly once , even if they appear in the middle of a function. That's the real problem.
Jul 19, 2011 at 3:13am Jul 19, 2011 at 3:13am UTC
jsmith: that's a great point. I didn't realize that. thnx. if it's initialized at every call, what's the point of having it static, right? cool.
Last edited on Jul 19, 2011 at 3:16am Jul 19, 2011 at 3:16am UTC
Jul 19, 2011 at 3:19am Jul 19, 2011 at 3:19am UTC
>> Remember that static variables are initialized exactly once
That's the REAL explanation. And makes perfect sense based on what I see. Thanks!
All others conjectures were unsubstantiated as all the variables are *ALWAYS* known.
Jul 19, 2011 at 3:55am Jul 19, 2011 at 3:55am UTC
All others conjectures were unsubstantiated as all the variables are *ALWAYS* known.
In the case when
CorP != CALL
in the first call to the function, then it is undefined behaviour. errx
itself is not known until it is initialized. Trying to initialize it to itself doesn't make sense.
@paolopiace I believe you meant to say this at one point: "Thanks for trying to help. I really appreciate it."
Last edited on Jul 19, 2011 at 4:11am Jul 19, 2011 at 4:11am UTC
Jul 19, 2011 at 7:39am Jul 19, 2011 at 7:39am UTC
@shackar: let me think. one Mississippi two Mississippi ... done? wait not yet :)
static int x = x;
;)
Last edited on Jul 19, 2011 at 7:41am Jul 19, 2011 at 7:41am UTC
Jul 19, 2011 at 3:33pm Jul 19, 2011 at 3:33pm UTC
>> I believe you meant to say this at one point: "Thanks for trying to help....
Certainly. I see I sounded rude. Sorry.
Still:
>> In the case when CorP != CALL in the first call to the function, then it is undefined behaviour.
Sure, in general that's right. But not here: The very first call to the function assures (CorP == CALL).
Thanks again, though.
Last edited on Jul 19, 2011 at 3:40pm Jul 19, 2011 at 3:40pm UTC