| Author |
Message |
bharaths_jois
Joined: 28 Dec 2006 Posts: 12 Location: Bangalore,Karnataka,India
|
07 May 2008 6:25 Global variables in embedded systems |
|
|
|
Let's consider an embedded system, whose software consists of global variables.
Q1 ) Which is the space allocated for the global variables? If RAM, please specify
which section of RAM.
Q2 ) A local variable is created and occupies the memory when the scope of the
program where the variable is declared is being executed. Right:?:
Similarly, when do these global variables occupy the memory?
Thanks in advance
Regards,
Bharath
|
|
| Back to top |
|
 |
dipal_z
Joined: 21 Apr 2005 Posts: 297 Helped: 32 Location: India
|
07 May 2008 7:00 Re: Global variables in embedded systems |
|
|
|
| bharaths_jois wrote: |
Let's consider an embedded system, whose software consists of global variables.
Q1 ) Which is the space allocated for the global variables? If RAM, please specify
which section of RAM. |
Global variables (for that matter any variable) is allocated from RAM. For gcc compiler uninitialized variables will go to .bss section and initialized variables goes to .data section. Section names will vary according to your compiler.
| bharaths_jois wrote: |
Q2 ) A local variable is created and occupies the memory when the scope of the
program where the variable is declared is being executed. Right:?:
|
Its correct, to be precise they are allocated off the stack.
| bharaths_jois wrote: |
Similarly, when do these global variables occupy the memory?
|
Space for global variables is reserved at the compile time itself. While loading the program c startup code will ensure that reserved space is initialized correctly.
|
|
| Back to top |
|
 |
bharaths_jois
Joined: 28 Dec 2006 Posts: 12 Location: Bangalore,Karnataka,India
|
07 May 2008 8:06 Re: Global variables in embedded systems |
|
|
|
| Thank you. Moving ahead with this, I learnt that a copy of what ever has to go to the RAM will be in the hex file and hence in the ROM. And the startup code, which runs during the start of the application mode of the system will copy this content of ROM to appropriate area of RAM, is this right?
|
|
| Back to top |
|
 |
dipal_z
Joined: 21 Apr 2005 Posts: 297 Helped: 32 Location: India
|
07 May 2008 8:20 Global variables in embedded systems |
|
|
|
That's correct, however for initialized variables hex file only contains information about how many bytes needs to be allocated in RAM.
So startup code will reserve that much space and fill the area with all zeros.
For initialized variable hex file contains the initialized data as well along with size in the case startup code will copy this data from hex file (ROM) to RAM.
|
|
| Back to top |
|
 |
bharaths_jois
Joined: 28 Dec 2006 Posts: 12 Location: Bangalore,Karnataka,India
|
07 May 2008 8:58 Re: Global variables in embedded systems |
|
|
|
Thanks again. How would the use of any storage class decide the space into which the variable would go? Also using a const, how would this matter? Where would these variables go (global and local)?
i.e where would a global const sit and where would a local const sit?
|
|
| Back to top |
|
 |
dipal_z
Joined: 21 Apr 2005 Posts: 297 Helped: 32 Location: India
|
07 May 2008 9:31 Global variables in embedded systems |
|
|
|
Constants are always stored in ROM/FLASH irrespective of whether they are local or global, however compiler will take care of limiting the scope of local constants.
It not possible to discuss implementation of constants in general as it depends on individual processor family and compilers.
|
|
| Back to top |
|
 |
sureshreddy
Joined: 12 May 2008 Posts: 42 Helped: 4 Location: BANGLORE
|
12 May 2008 14:05 Re: Global variables in embedded systems |
|
|
|
FOr gcc complier all global and static variables are stored in datasement.
if the variable is initialised then that variable comes under category of IDS(Initialised data segment).If the variable is not initialised then that variable comes under category of UIDS(Un initialised data segment).
|
|
| Back to top |
|
 |
FvM
Joined: 22 Jan 2008 Posts: 2219 Helped: 370 Location: Bochum, Germany
|
12 May 2008 17:04 Re: Global variables in embedded systems |
|
|
|
| With any compiler, you can get information about memory layout and location of specific variables, e. g. map files. As no target system or compiler has been told in the original question, I like to mention, that many things said in the discussion probably may be wrong. E. g. for a processor with a stack separated from data space (8051 or PIC), local variables typically aren't located on stack.
|
|
| Back to top |
|
 |