Global variable memory allocation

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

Reply to
ravikumar.n
Loading thread data ...

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.

formatting link

(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:

formatting link

- Richard

Reply to
Richard Willis

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).

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 (broeker@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
Reply to
Hans-Bernhard Broeker

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 
 "show options" at the top of the article, then click on the 
 "Reply" at the bottom of the article headers." - Keith Thompson
Reply to
CBFalconer

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).

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 (broeker@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
Reply to
Hans-Bernhard Broeker

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.