Question about memory Sections (.text, .data, .rodata, .bss)

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In the following code:

#include <something.h>

const int a=100;

int b=200;

int c;

void myFunc(int d)

{
   int e=20;

   int f;

   ...
 }

 

Q1. const int a will be in .rodata? Will it be copied to RAM by cstartup code? If not, how would the program access this const variable?
Q2. int b is stored in which section? It will be copied to RAM .data by cstartup code?

Q3. int c will be in .data (RAM), and initialized to zero by cstartup code?

Q4. When myFunc(5) is executed, int d (which is 5), int f will be in stack?

Q5. How about int e? It is initialized, so should it be in .data? But it is also a local variable, so it should be in stack?

Last Edited: Sat. May 13, 2017 - 08:35 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1. Const will most likely be accessed in place - ie from flash. With the ARM, the ram and flash exist in the same address space but just at different addresses. This is in contrast to the AVR as one example where the flash and ram exist in different data spaces, so flash access is 'special'.
2. B will be initialised to 200 from a table in flash when the cstartup executes.
3. Yes
4. I'd guess as the ARM has a number of 32 bit registers that those vars would exist in the registers. Depends on the rest of the code and the compiler.
5. E is a local var. it might live on the stack or in a register - much the same as above.

Rather than second guess what the compiler might do, look at the assembler the compiler generates. For your exact example, the compiler might choose to get rid of your variables as they are not used!
As you've not mentioned what compiler - note that different compilers may do different things.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Kartman, 

2. B will be initialised to 200 from a table in flash when the cstartup executes.

I read from somewhere that int b will reside in a section ".data" in flash, and during cstartup, it is copied to .data in RAM. And therefore an initialized variable actually occupy double the space!

Does this make sense?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There's a price to pay for everything! Can you think of a means of initialising a variable without consuming memory?