Variable Address Assignment

Thread Starter

gogo00

Joined Oct 28, 2023
43
I'm trying to figure out who assigns addresses to variables when a C program is turned into an executable file. Compiler or linker. I think it's the task of the linker. If variables are in a single source file or across multiple files in a project, the linker should assign addresses to them
 

WBahn

Joined Mar 31, 2012
32,834
That depends on what kinds of variables you are talking about. It also depends on choices made by whomever write the compiler.
 

nsaspook

Joined Aug 27, 2009
16,322
I'm trying to figure out who assigns addresses to variables when a C program is turned into an executable file. Compiler or linker. I think it's the task of the linker. If variables are in a single source file or across multiple files in a project, the linker should assign addresses to them
It's mainly the task of the linker but you can assign addresses to variables in C on the vast majority (as a non-portable specific implementation extension/feature of the C standard) of compilers that include or are targeted to embedded programming like xc32 for the PIC32 controller.

C:
/* This array is the offscreen frame buffer used for rendering.
 ** It isn't possible to read back frome the OLED display device,
 ** so display data is rendered into this offscreen buffer and then
 ** copied to the display.
 *  must be in uncached memory for pic32 DMA so use __attribute__((coherent))
 * DMA0 SPI TX transfers DATA, CMD
 * DMA1 GLCD buffer transfers
 * DMA2 SPI TX transfers CMD, NOT USED
 */
#ifdef __32MK0512MCJ048__    // NO bank 2 for this CPU so memory is in bank 1
uint8_t __attribute__((address(BANK1), coherent)) rgbOledBmp0[cbOledDispMax]; // two display buffers for page flipping
uint8_t __attribute__((address(BANK1 + cbOledDispMax), coherent)) rgbOledBmp1[cbOledDispMax];
#ifdef USE_DMA
static uint8_t __attribute__((address(BANK1 - 8), coherent)) rgbOledBmp_blank[4] = {0x00, 0x00, 0x00, 0x00}; // 32-bit frame-buffer clearing variable
#endif
volatile uint8_t __attribute__((address(BANK1 - 16), coherent)) rgbOledBmp_page[5];
#endif

#ifdef __32MZ1025W104132__    // bank 2 for this CPU
uint8_t __attribute__((address(BANK2), coherent)) rgbOledBmp0[cbOledDispMax]; // two display buffers for page flipping
uint8_t __attribute__((address(BANK2 + cbOledDispMax), coherent)) rgbOledBmp1[cbOledDispMax];
#ifdef USE_DMA
static uint8_t __attribute__((address(BANK2 - 8), coherent)) rgbOledBmp_blank[4] = {0x00, 0x00, 0x00, 0x00}; // 32-bit frame-buffer clearing variable
#endif
volatile uint8_t __attribute__((address(BANK2 - 16), coherent)) rgbOledBmp_page[5];
#endif
The linker will use those __attribute__ addresses and conditions in the C source code to allocate the correct type of memory for the variables requested at the address requested.
 

MrChips

Joined Oct 2, 2009
34,810
Besides the compiler and linker, there is still another way addresses can be assigned, and that is dynamically at run time.
Even after the application is compiled and linked into an executable file, most systems run under a supervisory operating system. Applications are relocatable, i.e. they can be loaded at any available location in the application heap. From there, the main program and subroutines will use the space allocated to them and variable address are assigned relative to a stack pointer.
 

Thread Starter

gogo00

Joined Oct 28, 2023
43
That depends on what kinds of variables you are talking about. It also depends on choices made by whomever write the compiler.
Which factor determines where a variable gets stored in memory? Does it depend on the storage classes in the C language or the memory layout of hardware, or both? I believe it's dependent on both factors because the storage classes help the compiler determine where to place variables in memory.

Take GCC compiler

Here's a list of common variable types:

1. Local variable
2. Global variable
3. Static Global variable
4. Static local variable
5. Extern variable
6. Pointer variable
7. Volatile variable

@WBahn What's your point on this
 
Last edited:

nsaspook

Joined Aug 27, 2009
16,322
Which factor determines where a variable gets stored in memory? Does it depend on the storage classes in the C language or the memory layout of hardware, or both? I believe it's dependent on both factors because the storage classes help the compiler determine where to place variables in memory.

Take GCC compiler

Here's a list of common variable types:

1. Local variable
2. Global variable
3. Static Global variable
4. Static local variable
5. Extern variable
6. Pointer variable
7. Volatile variable
What do you think those factors are for each type you listed?
 

Thread Starter

gogo00

Joined Oct 28, 2023
43
If the hardware memory is split into four segments - program memory, data memory, stack memory, and heap memory -

here's where different types of variables typically reside:

1. Local variable: Stack memory
2. Global variable: Data memory
3. Static Global variable: Data memory
4. Static local variable: Data memory
5. Extern variable: Data memory
6. Pointer variable: no idea
7. Volatile variable: no idea
 

MrChips

Joined Oct 2, 2009
34,810
If the hardware memory is split into four segments - program memory, data memory, stack memory, and heap memory -

here's where different types of variables typically reside:

1. Local variable: Stack memory
2. Global variable: Data memory
3. Static Global variable: Data memory
4. Static local variable: Data memory
5. Extern variable: Data memory
6. Pointer variable: no idea
7. Volatile variable: no idea
On most computer systems, memory is SRAM (most MCU based systems) and DRAM (most PCs).
Hence all of the above are in the one and same memory.
 
Top