Compile Time, Runtime Memory

Thread Starter

gogo00

Joined Oct 28, 2023
37
Dear Community,

I've been trying to wrap my head around the concepts of compile time and runtime in C programming, particularly in relation to memory allocation for variables. My understanding is a bit fuzzy, and I hope to gain some clarity. My current understanding is that during compilation, the compiler assigns memory to variables (compile time allocation), while during program execution on hardware, memory is allocated for variables (runtime allocation). However, I'm confused because it seems that the actual memory allocation occurs when the program is running, which makes me question whether both cases are technically considered runtime allocation.

Could someone provide a clearer distinction between compile time and runtime memory allocation in C programming?
 

WBahn

Joined Mar 31, 2012
30,048
You are correct that the actual act of allocating memory to a variable can't happen until runtime. But since that is the case, we are concerned with when the decisions about where that memory will be allocated are made. In compile-time allocation. For instance, global variables are generally located at the end of the code itself, such that just loading the program into memory allocates and initializes these variables.
 

MrChips

Joined Oct 2, 2009
30,795
It depends on the complexity of the application, the software, and the software environment (i.e. operating system).

On a small embedded MCU (microcontroller unit) the programmer can declare all memory assignments in code. Hence memory assignments are done at compile time.

If the program uses subroutines, then the common practice is to allocate space for local variables from the stack. This is obviously allocated at run time.

For more complex applications running under an operating system, all code space and variable space is allocated by the operating system. Hence memory allocation is done at run time.
 

Thread Starter

gogo00

Joined Oct 28, 2023
37
I still don't have clear idea my understanding is that when code is compiled, the compiler analyzes it and determines the memory needed for these variables based on their types (int and float).

For instance, an int typically requires 4 bytes of memory and a float usually takes up 4 bytes as well. So, during compilation, the compiler generates instructions to allocate memory for int and float to ensure that when the program is executed, the required memory space for these variables is reserved in the target's memory. This allows the program to use these variables to store and manipulate values during runtime.
 

MrChips

Joined Oct 2, 2009
30,795
I still don't have clear idea my understanding is that when code is compiled, the compiler analyzes it and determines the memory needed for these variables based on their types (int and float).

For instance, an int typically requires 4 bytes of memory and a float usually takes up 4 bytes as well. So, during compilation, the compiler generates instructions to allocate memory for int and float to ensure that when the program is executed, the required memory space for these variables is reserved in the target's memory. This allows the program to use these variables to store and manipulate values during runtime.
In a small embedded system, the programmer might choose the physical location of the variable. Hence memory allocation is done at compile time.

In a larger multi-tasking environment, there could be many different applications running at the same time. Hence it is imperative that all code and data space is relocatable, i.e. the program and data can be located at any memory location. Hence all program and data storage is allocated at run time. The program code does not need to know the physical location of the data. Data addressing is done relative to some reference location assigned by the operating system (i.e. via application heap address, stack pointers, segment registers, etc.)
 

Thread Starter

gogo00

Joined Oct 28, 2023
37
ing to understand the two primary systems:

1. Code directly running on the embedded system.
2. Code running on the embedded system under the operating system.

Currently, I'm focused on comprehending the first system, where the code is compiled by a compiler and translated into an executable file that directly operates on the microcontroller. (e.g., AVR)

My question pertains to variable allocation within this context. When defining variables, we inform the compiler about the intended data to be stored. The compiler then allocates memory for these variables based on their data types. However, while the compiler determines memory allocation, it doesn't explicitly specify the memory location.

I'm intrested if the C standard decide the specific memory allocation location or if this responsibility falls under the hardware architecture.
 

WBahn

Joined Mar 31, 2012
30,048
The C standard doesn't have much to say about when and how memory is allocated. It specifies behaviors that must be exhibited when the code is executed. For instance, nowhere in the standard does the word 'stack' or 'heap' appear.
 

MrChips

Joined Oct 2, 2009
30,795
In a small stand alone embedded application, the linker will allocate data storage starting at a given SRAM address.
The programmer can force absolute addresses if so desired. In this case, the compiler/assembler will assign the data to that absolute address.

The specific coding for absolute addressing depends on the compiler.
For example, it could be as follows:

int16_t mydata @ 0x0400; // set absolute address of mydata
 
Top