This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Automatically initialize all global variables/arrays in GCC.
- From: "Michael Meissner" <michael dot meissner at amd dot com>
- To: "Kevin Hung" <cykhung at gmail dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Fri, 6 Jul 2007 14:12:42 -0400
- Subject: Re: Automatically initialize all global variables/arrays in GCC.
- References: <11e3abec0707051320o3d2e01c5y917a1b947b5b5087@mail.gmail.com>
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@amd.com