On 2013-10-22, Steven D'Aprano <steve+comp.lang.pyt...@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?
Firstly, it's not non-global arrays that have undefined contents. It's _auto_ arrays that have undefined contents. void foo(void) { int a[4]; // non-global, _auto_ variable and has undefined contents } void bar(void) { static int a[4]; // non-global, _static_ variable initially 0's } As to _why_ it's that way, you'd have to ask the guys who decided that. I supect it's because zeroing static variables involves very little run-time overhead, while zeroing auto variables could cause huge amounts of overhead if that auto variable is declared inside the innermost of nested loops. [Presumably a good optimizing compiler would not zero an auto variable that was always set before it was referenced, but it takes a lot of smarts for compiler to figure that out correctly 100% of the time -- probably more smarts than a PDP-11 had room for.] -- Grant Edwards grant.b.edwards Yow! Let's send the at Russians defective gmail.com lifestyle accessories! -- https://mail.python.org/mailman/listinfo/python-list