Python Front-end to GCC

Oscar Benjamin oscar.j.benjamin at gmail.com
Tue Oct 22 13:37:54 EDT 2013


On 22 October 2013 18:23, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Tue, 22 Oct 2013 16:53:07 +0000, Frank Miles wrote:
>
> [snip C code]
>> What you're missing is that arr[] is an automatic variable.  Put a
>> "static" in front of it, or move it outside the function (to become
>> global) and you'll see the difference.
>
> Ah, that makes sense. Thanks to everyone who corrected my
> misunderstanding.
>
> Well, actually, no it doesn't. I wonder why C specifies such behaviour?
> Why would you want non-global arrays to be filled with garbage?

It's not that you want them filled with garbage but rather that you
know you will fill them with useful values later and you don't want to
waste time pre-filling them with zeros first. Or perhaps you have some
other way of knowing which values are to be considered initialised.
This is reasonable in some contexts.

OTOH why in particular would you want to initialise them with zeros? I
often initialise arrays to nan which is useful for debugging. It means
that you can see if you made a mistake when you were supposed to have
initialised everything to useful values. In many contexts it would be
difficult to distinguish between a valid zero and a zero because you
haven't yet inserted a value. This kind of error can show up more
quickly if you don't zero the memory since the uninitialised values
will often be out of range for what you expected and will give very
noticeable results (such as a seg-fault).


Oscar



More information about the Python-list mailing list