Question on memory allocation in program memory

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

Hi all,

I know these will allocated to program memory section, but not sure are these allocated to .text section ? or any section on program memory ?

unsigned char var1[] = "Is this initialized value allocated to .text section ?";

unsigned char var2[] PROGMEM = "Is var2 allocated to .text section ?";

Because after read this.
http://www.nongnu.org/avr-libc/u...
It seem only .text section available in program memory ?

Regards,

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

firmware wrote:
I know these will allocated to program memory section, but not sure are these allocated to .text section ?
The initialization characters for both of the definitions end up in Flash memory but in different places. In the first case, the way that the array is defined will cause it to ultimately be in RAM but the initialization data for it is stored in Flash and copied to RAM at startup.

In the second case, the array itself is located in Flash but it is in a special section (called .lowtext, I think) that is placed in Flash preceding the code.

You should be aware that you have to write your code differently to access the two arrays. I believe that there is a tutorial on the subject.

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

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

You need to read and understand (ld docs) the linker script to get the full picture, but short answer-

Your first example- this is not code, so it goes into the .data section (in data memory). And since this is initialized data, it has to be stored somewhere and loaded into ram at runtime. To do this, the .data section also gets a LMA (load memory address) which is located at the end of the .text section (in text memory). So you have this var actually in 2 different sections and memory areas. The C runtime code inits the ram with the data stored in flash, and the running program only cares about the .data addresses (ram).

Your second example is also data, but you have now told it you want it in a specific section '.progmem' instead of the default '.data' section. That section is in the .text section (in text memory), so it will be stored in flash (look at the linker script to find where .progmem is located).

Notice in the linker script there is .text, .data sections, and also text, data 'memory' regions. Can be a little confusing.