Python Front-end to GCC

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Oct 22 11:22:59 EDT 2013


On Tue, 22 Oct 2013 14:04:57 +0000, Dave Angel wrote:

[...]
> I agree with most of what you say in the message, 

Glad to hear I wasn't completely full of it. As a non-C developer, I'm 
very conscious that a lot of what I know about C is second hand.


> but here you go on to
> say the C code is unsafely skipping initialization, which is not the
> case.

Are you talking generically, or specifically about the C code referenced 
in the link I gave?

"Memory is always zeroed" is one of the advantages of Go over C and C++, 
at least according to Rob Pike:

http://commandcenter.blogspot.com.au/2012/06/less-is-exponentially-
more.html



> By the way, a C compiler typically handles any initialization of a
> static variable the same way.  So if you declare and initialize a static
> variable as
> 
> int  myvar = 34 * 768;
> 
> it'll put the product directly in the executable image, and no runtime
> code is generated.

Yes, that's a keyhole optimization, CPython does the same sort of thing:

py> import dis
py> dis.dis(compile("myvar = 34 * 768", "", "exec"))
  1           0 LOAD_CONST               3 (26112)
              3 STORE_NAME               0 (myvar)
              6 LOAD_CONST               2 (None)
              9 RETURN_VALUE



> Perhaps you were thinking of an automatic variable, which is not
> initialized unless the programmer says so, and is then initialized with
> code.

No, I was thinking of an array. Arrays aren't automatically initialised 
in C.


-- 
Steven



More information about the Python-list mailing list