Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Global variables in embedded systems

Status
Not open for further replies.

bharaths_jois

Newbie level 6
Joined
Dec 28, 2006
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Bangalore,Karnataka,India
Activity points
1,374
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
 

bharaths_jois said:
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 said:
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 said:
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.
 

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?
 

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.
 

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?
 

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.
 

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

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.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top