Global variable memory allocation

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View


Hi all,
        I am Ravi Kumar.N, I have a query regarding memory allocation
for global variable.


  1) I know that for global variable memroy will be allocated during
compilation process and the value assigned for that variable will be
zero.

 2) If we explicitly allocate some known value to the global variable,
the assignment of that value to the corresponding variable  will take
place during compilation process or does it take place during execution
of the program.

Please let me know.


With regards,
       Ravi Kumar.N


Re: Global variable memory allocation
Quoted text here. Click to load it

Assuming you are compiling for an embedded target and assuming that you
are storing the firmware in Flash in binary form...

The linker will be directed by you to place preset variables in the
binary file. You must then (using startup code) copy this memory to the
RAM location that you specified it to run at. The global and static
variables are just a location in RAM, but the location must be zero'd by
you, also done in the startup code (eg. crt.s).

So to answer your question, the variable is preassigned by the compiler
or linker by generating a block of data in the binary file that
represents the values for each pre assigned variable. It is left up to
you to copy this chuck of data to RAM before calling main().

Of course if you are running an operating system, then the OS does all
this for you and the binary file is actually a structured file format
that directs the OS in setting itself up for execution, ELF and COFF are
two such examples of these.

Notes:
(1) For GCC, the output is usually an ELF file. This must be translated
into a binary file by you, using 'objcopy' in the binutils.

(2) For information on configuring the GCC linker, 'ld' scripting
information. http://sourceware.org/binutils/docs-2.16/ld/index.html

(3) The LPC2000 Eclipse HowTo is an invaluable resource that I believe
can be used for any target. It describes the steps involved in
generating startup code and the linker script:
http://www.olimex.com/dev/pdf/ARM%20Cross%20Development%20with%20Eclipse.pdf

  - Richard




Re: Global variable memory allocation

Quoted text here. Click to load it

Zero, or whatever else you initialized it to.  And this happens not
only for "global" variables (a concept the C language doesn't actually
have), but to all variables of what the languages "static storage
duration".  That includes variables defined inside code blocks but
qualified "static", and all variables defined external to all code
blocks (so-called "external" objects).

Quoted text here. Click to load it

First, a terminology nitpick: initialization of static objects is not
actually called an "assignment".  The two words mean different things.

It cannot really be said when exactly initialization happens.  The
final decision that it's going to be done is made by the
compiler+linker, but the last activity is usually carried out by the
run-time environment, at some opportune moment (usually directly on
start-up).  It can be the equivalent of a series of automatically
generated simple assignments, or single block copy of all values from
the program file's stored version directly to the actual variables
(the "data segment" approach), or it can be something completely
different.

This is not something an ordinary C programmer would ever have to
worry about.  It's the job of whoever implemented the C compiler for
the given target platform to get this done in whatever way he pleases.
The programmer can assume that by the time a variable is first
referred to, the iniatilization has been carried out.

Only in the embedded world, where people commonly have to at least
check the start-up code and runtime environment, and possibly adapt
then to their particular hardware, this promise is not always kept.
It's still the compiler maker's business to document the necessities,
though, even in this case.  The language definition has nothing to say
about this.

--
Hans-Bernhard Broeker ( snipped-for-privacy@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.

Re: Global variable memory allocation
Quoted text here. Click to load it

All you know, and all you need to know, is that the value is
initialized before your program gets control.  It's all in the
standard, which see.

--
"If you want to post a followup via groups.google.com, don't use
 the broken "Reply" link at the bottom of the article.  Click on
We've slightly trimmed the long signature. Click to see the full one.
Re: Global variable memory allocation

Quoted text here. Click to load it

Zero, or whatever else you initialized it to.  And this happens not
only for "global" variables (a concept the C language doesn't actually
have), but to all variables of what the language calls "static storage
duration".  That includes variables defined inside code blocks but
qualified "static", and all variables defined external to all code
blocks (so-called "external" objects).

Quoted text here. Click to load it

First, a terminology nitpick: initialization of static objects is not
actually called an "assignment".  The two words mean different things.

It cannot really be said when exactly initialization happens.  The
final decision that it's going to be done is made by the
compiler+linker, but the last activity is usually carried out by the
run-time environment, at some opportune moment (usually directly on
start-up).  It can be the equivalent of a series of automatically
generated simple assignments, or single block copy of all values from
the program file's stored version directly to the actual variables
(the "data segment" approach), or it can be something completely
different.

This is not something an ordinary C programmer would ever have to
worry about.  It's the job of whoever implemented the C compiler for
the given target platform to get this done in whatever way he pleases.
The programmer can assume that by the time a variable is first
referred to, the iniatilization has been carried out.

Only in the embedded world, where people commonly have to at least
check the start-up code and runtime environment, and possibly adapt
then to their particular hardware, this promise is not always kept.
It's still the compiler maker's business to document the necessities,
though, even in this case.  The language definition has nothing to say
about this.

--
Hans-Bernhard Broeker ( snipped-for-privacy@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.

Site Timeline