|
From: Chris S. <c.s...@co...> - 2005-01-24 04:02:44
|
On Sun, Jan 23, 2005 at 10:14:47PM -0500, Albert Cahalan wrote:
> if(foo) foo=0; // foo was uninitialized
>
> This code is rather odd I admit, but it is has perfectly
> well-defined behavior. It initializes foo to 0, skipping
> the store if foo is already 0.
No, it does NOT have well-defined behavior. It has a well-defined
post-condition - big difference. For example:
{
int foo;
if (foo)
do { foo++ } while (foo < MAX_INT);
foo = 0;
}
Has exactly the same well-defined post-condition as your example,
i.e. foo==0, and it also has exactly the same _undefined_ (depending
on stack-garbage) behavior. You cannot predict either code's actual
execution path, which IS its behavior.
Anyway, 'if (foo) foo=0' is more than just ill-defined, it's ugly: use
'foo=0'
-chris
|