When the program starts, it is allocated memory to the stack area. Declaring a variable uses this stack area memory. Stack area memory is determined how much it will be used when compiled, and stack area memory is accessed at a very high speed. Use caution because using too large an array can run out of stack area memory.
void a()
{
if(true) {
int x = 0;
}
x = 1;
}
In this example, x is the variable that uses stack area memory.
On the other hand, heap area memory is used when memory is dynamically allocated while running. Heap area memory can be allocated large memory instead of being a little slow to read. And since it remains in the memory area before it is free, it can be used outside the allocated block.
int *x;
void b()
{
if(true) {
x = malloc(sizeof(int));
}
*x = 1;
}
void c()
{
free(x);
}
If you are allocated memory by malloc like this, you will receive it in the heap area, and you can continue to use the memory allocated to b until it is free in c.
© 2024 OneMinuteCode. All rights reserved.