OT: Requesting C advice

Mike McCarty Mike.McCarty at sbcglobal.net
Mon Jun 4 21:32:42 UTC 2007


Les wrote:
> On Fri, 2007-06-01 at 19:02 -0400, Matthew Saltzman wrote:
> 
[snip]

Maybe I have misunderstood you. Are you saying that the compiler alone
is not enough? If so, then I agree with you. However, if you mean that
the initial state of statically allocated variables is not known at
program start up, then I have to say that I believe you are incorrect.

>>But I confess, I don't see how this code proves your point.  It does 
>>demonstrate that globals are initialized by default, though.
>>
> 
> Actually, it doesn't.  And this is the problem.  Many people assume that
> because they obtained 0 one time, that the value was set in memory by
> some behind the scenes action of the compiler.  In fact the memory could

Not the compiler, the implementation. The implementation is required
by the Standard to initialize all statically allocated variables to
certain values before invoking the designated C function (usually
main(.,.)).

[QUOTE MODE ON]
5.1.2  Execution environments

        [#1] Two execution environments  are  defined:  freestanding
        and  hosted.   In  both cases, program startup occurs when a
        designated  C  function   is   called   by   the   execution
        environment.    All  objects  in  static  storage  shall  be
        initialized (set to their  initial  values)  before  program
        startup.   The  manner and timing of such initialization are
        otherwise unspecified.  Program termination returns  control
        to the execution environment.
[QUOTE MODE OFF]

> have been set by any of a number of actions.  Some memory chips start
> with all data zero'ed (at the output, at the physical layer the
> construction is designed to minimize current drain and transitions, but
> that is another topic entirely.)  In that case, if power had been off
> all memory not explicitly set would be zero by default.  Another

This is irrelevant. Les, I've supported real time systems that behave as
you describe, and try to "restart" rather than "start again" in the
interests of increasing availability. That's useful, but non-conforming
behavior.

In fact, one system I supported had three levels of startup:
initial (reload all programs and start them from scratch), hard restart
(using the program images in memory, restart them from scratch),
and soft restart (restart the initial task in the programs, but do not
attempt to re-initialize everything). The restarts were escalated
from soft to hard to initial. About 50% of the time soft restarts would
work, and about 98% of the time hard restarts would work. Some programs
simply were marked "do not attempt restart" as they did significant
H/W initialization, and could not hope to disentangle the current state.
A system with such a program simply never tried anything but hard
restarts and initial restarts.

> situation is when a memory checker runs, and leaves memory in a zero
> state (most do by design).  Thus if the compiler doesn't initialize
> memory, and the memory where the code is placed has not been used in a
> prior run, the variable space will be zero.  But if the program is

What you've just proven is that the implementation must do some work
before invoking the designated C function. I've worked on such systems.
In fact, you might look into the meaning of USS and BSS.

[snip]

>     So, while the compiler may initialize the variables, there are other
> issues that can impact the true state at run time, and therefore default
> state should not be relied on as the condition.  After all, you create a

The Standard is (intended to be) written such that what you describe
cannot happen.

> variable to store information, don't you?  Why would you not iinitialize
> it?  Anyway, while this has been a good discussion, I hope that you have

Because the implementation already initializes it for you. There are
two ways:

	SomeType_t	SomeVar = SomeValue;
	AnotherType_t	AnotherVar;

The implementation "knows" of efficient ways on the target architecture
to accomplish what you want. In the first place, it probably puts
all statically allocated variable into a special region, and then
uses a block copy instruction to copy the entire initial values
all in one fell swoop. In the second, it probably puts the
statically allocated variable into a region whose size is simply noted
in the object code, and this region is initialized by using some sort
of fill instruction.

Mike
-- 
p="p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
Oppose globalization and One World Governments like the UN.
This message made from 100% recycled bits.
You have found the bank of Larn.
I can explain it for you, but I can't understand it for you.
I speak only for myself, and I am unanimous in that!




More information about the fedora-list mailing list