OT: Requesting C advice
mjs at clemson.edu
Fri Jun 1 00:50:32 UTC 2007
On Thu, 31 May 2007, Mike McCarty wrote:
> Les wrote:
>> On Thu, 2007-05-31 at 05:52 -0500, Mike McCarty wrote:
>>> Andy Green wrote:
>>>> Mike McCarty wrote:
>>>>> C doesn't initialize what? It initializes all used variables.
>>>> Not if they're on the stack. You should get a compiler warning
>>>> nowadays... but don't count on it!
>>> Erm, C knows nothing about a "stack". However, it is true
>>> that automatic variable are not necessarily initialized.
>>> I should have stated that all statically allocated variables
>>> are initialized. Thanks for the correction.
>> That must be something recent. Also some people believed that it did in
> IIRC, it was mentioned in K&R v1.
My K&R1 is lost to history 8^(, but my H&S 3rd edition spent a lot of
space on the difference between "traditional" or "K&R" and ANSI compilers.
In the section on default initialization the only static vars that were
*not* required to be initialized in traditional compilers were unions.
The note says that some traditional compilers would not permit such
initializations. The ANSI requirement is that the union be initialized to
the default for the first element.
The other note was that some traditional compilers would initialize a
static float/double to all-bits-zero, even if that was not the
representation of 0.0. That's not permitted in ANSI C.
>> This part I am sure of, because I have had to fix many, many peoples
>> code due to this belief. The ANSI comittee may have changed the
>> standard, but I would bet that a lot of older compilers still generate
>> code with no initialization.
> Older? ANSI C is since 1989. I guess one could characterize 19 years
> as "older". :-)
And those older-than-dirt compilers were even broken for their age if
that's the way they behaved.
Clemson University Math Sciences
mjs AT clemson DOT edu
More information about the users