Re: Automatically initialize all global variables/arrays in GCC.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Jul 05, 2007 at 01:20:47PM -0700, Kevin Hung wrote:
> I am using gcc to compile our C code under Linux. After compiling and
> linking, we use SWIG to create python wrapper for the C code so that
> we can execute C functions in python.
> 
> My question is: When our C program gets loaded into python, is it
> possible to have all global variables/arrays in our C code to be
> initialized to some magic constants (say all ones)? I think if this is
> possible, it would be a linker option under GCC. My gcc version is:

The ISO C standard mandates that all static/global variables that aren't
otherwise initialized are initialized to 0.  Under Linux, variables that aren't
explicitly initialized get put into the bss sections that aren't allocated
space in the disk image, but the OS guarantees will be set to 0 when you touch
the page.

In general, you probably don't want to do something like zap the bss area in a
startup routine, because you don't know what other modules might have put stuff
there.

If you want to initialize a static/global to a value, you should create the
definition with an initializer.

-- 
Michael Meissner, AMD
90 Central Street, MS 83-29, Boxborough, MA, 01719, USA
michael.meissner@xxxxxxx



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux