Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!nntp.club.cc.cmu.edu!micro-heart-of-gold.mit.edu!bloom-beacon.mit.edu!panix!not-for-mail From: Grant Edwards Newsgroups: comp.lang.python Subject: Re: Python Front-end to GCC Date: Tue, 22 Oct 2013 18:49:58 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 41 Message-ID: References: <4012031f-5334-4be8-a673-e0d8c8917fb2@googlegroups.com> <5264dbbe$0$30000$c3e8da3$5496439d@news.astraweb.com> <5265bba8$0$29981$c3e8da3$5496439d@news.astraweb.com> <526668e5$0$29981$c3e8da3$5496439d@news.astraweb.com> <52669852$0$29981$c3e8da3$5496439d@news.astraweb.com> <5266aa80$0$29981$c3e8da3$5496439d@news.astraweb.com> <5266b496$0$29981$c3e8da3$5496439d@news.astraweb.com> NNTP-Posting-Host: dsl.comtrol.com X-Trace: reader1.panix.com 1382467798 17555 64.122.56.22 (22 Oct 2013 18:49:58 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Tue, 22 Oct 2013 18:49:58 +0000 (UTC) User-Agent: slrn/1.0.1 (Linux) Xref: csiph.com comp.lang.python:57299 On 2013-10-22, Steven D'Aprano 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!