init of static var

I discovered the following.

The conditional assignment following the declaration does never have effect:

int static errx = (CorP == CALL ? 0 : errx);

Although often the condition is true, errx = 0 never happens and always comes errx = errx (never changes).

In the exact same context, this 'if' statement following the declaration works fine:

1
2
int static errx;
if (CorP == CALL) errx = 0;


errx = 0 happens when the condition is true. Otherwise errx does not change.

I'd appreciate an explanation why the 1st does not work while the 2nd does.

I'm compiling with gcc 4.4.3. No warnings.

Thanks!
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
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?

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
Remember that static variables are initialized exactly once, even if they appear in the middle of a function. That's the real problem.
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
>> 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.
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
@shackar: let me think. one Mississippi two Mississippi ... done? wait not yet :)
static int x = x; ;)
Last edited on
>> 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
Topic archived. No new replies allowed.