Products > Programming
Memory Allocation in C: Compile Time vs. Run Time
Kittu20:
Hello everyone,
I'm trying to wrap my head around the concept of memory allocation at compile time versus run time. My understanding is that memory for variables isn't allocated until the program runs on a PC. I belive that memory allocation happens at runtime. However, I'm confused about how memory allocation can occur at compile time when the program doesn't actually run on hardware(PC)
consider code
--- Code: ---#include <stdio.h>
#include <stdlib.h>
// Global variable
int globalVar = 10;
// Static global variable
static int staticGlobalVar = 20;
int main() {
// Local variable
int localVar = 5;
// Static local variable
static int staticLocalVar = 15;
// Dynamic memory allocation using pointers
int *dynamicVar = (int *)malloc(sizeof(int));
if (dynamicVar == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
*dynamicVar = 25;
// Extern variable (defined in another source file)
extern int externVar;
// Pointer variable
int *pointerVar;
pointerVar = &globalVar;
printf("Global variable: %d\n", globalVar);
printf("Static global variable: %d\n", staticGlobalVar);
printf("Local variable: %d\n", localVar);
printf("Static local variable: %d\n", staticLocalVar);
printf("Dynamic variable: %d\n", *dynamicVar);
printf("Extern variable: %d\n", externVar);
printf("Pointer variable (points to globalVar): %d\n", *pointerVar);
// Free dynamically allocated memory
free(dynamicVar);
return 0;
}
--- End code ---
Could you please clarify this concept of memory allocation in C programs?
tggzzz:
--- Quote from: Kittu20 on September 29, 2023, 09:15:18 am ---I'm trying to wrap my head around the concept of memory allocation at compile time versus run time. My understanding is that memory for variables isn't allocated until the program runs on a PC. I belive that memory allocation happens at runtime. However, I'm confused about how memory allocation can occur at compile time when the program doesn't actually run on hardware(PC)
...
Could you please clarify this concept of memory allocation in C programs?
--- End quote ---
Yet another poor question :( See https://entertaininghacks.wordpress.com/library-2/good-questions-pique-our-interest-and-dont-waste-our-time-2/
Your code example is useless, because you have just copied it from somewhere. You need to explain what you think will happen; only after that can anybody here help you correct your misunderstanding.
Yes, we can help: this is the same for every computer language, and is well described in textbooks. Textbooks will explain this subject better than quickly scribbled responses on a forum.
Textbooks about C are widely available in India, in many of the languages used in India.
IanB:
Maybe you are familiar with the game of Monopoly? The rules of the game allocate a certain amount of money to each of the players to start with. How can the rules allocate money to the players before the game has even begun?
Kittu20:
--- Quote from: tggzzz on September 29, 2023, 09:30:56 am ---
--- Quote from: Kittu20 on September 29, 2023, 09:15:18 am ---I'm trying to wrap my head around the concept of memory allocation at compile time versus run time. My understanding is that memory for variables isn't allocated until the program runs on a PC. I belive that memory allocation happens at runtime. However, I'm confused about how memory allocation can occur at compile time when the program doesn't actually run on hardware(PC)
...
Could you please clarify this concept of memory allocation in C programs?
--- End quote ---
Yet another poor question :( See https://entertaininghacks.wordpress.com/library-2/good-questions-pique-our-interest-and-dont-waste-our-time-2/
Your code example is useless, because you have just copied it from somewhere. You need to explain what you think will happen; only after that can anybody here help you correct your misunderstanding.
Yes, we can help: this is the same for every computer language, and is well described in textbooks. Textbooks will explain this subject better than quickly scribbled responses on a forum.
Textbooks about C are widely available in India, in many of the languages used in India.
--- End quote ---
Thank you for taking the time to respond to my question, and I appreciate your feedback. In my studies of memory allocation in C programs, I've come across the concept of memory allocation happening both at compile time and run time. My confusion arises from the fact that during compilation, the program isn't actually executed on hardware. So, I'm trying to undrstand how memory allocation can occur at compile time when there's no physical execution of the program.
The code I shared was meant to serve as an initial reference point to frame my question regarding memory allocation. Specifically, I intended to gain a better understanding of how memory allocation works in Compile Time vs. Run Time
Jeroen3:
--- Quote ---However, I'm confused about how memory allocation can occur at compile time when the program doesn't actually run on hardware(PC)
--- End quote ---
https://devconnected.com/understanding-processes-on-linux/
On targets without OS the process is statically linked, that means that the program has explicit references of the target hardware inside.
eg: the programs knows it's .TEXT (rom) will be at 0x0000 and memory .DATA (ram) will be at 0x8000 for example.
Whatever happens next is on your runtime, and implementation of that.
https://www.gnu.org/software/libc/manual/html_node/Memory-Allocation-and-C.html
--- Quote ---The C language supports two kinds of memory allocation through the variables in C programs:
- Static allocation is what happens when you declare a static or global variable. Each static or global variable defines one block of space, of a fixed size. The space is allocated once, when your program is started (part of the exec operation), and is never freed.
- Automatic allocation happens when you declare an automatic variable, such as a function argument or a local variable. The space for an automatic variable is allocated when the compound statement containing the declaration is entered, and is freed when that compound statement is exited.
In GNU C, the size of the automatic storage can be an expression that varies. In other C implementations, it must be a constant.
A third important kind of memory allocation, dynamic allocation, is not supported by C variables but is available via GNU C Library functions.
--- End quote ---
This should help you find relevant keywords to find literature.
Navigation
[0] Message Index
[#] Next page
Go to full version