C has 4 different pools of memory.
Static memory persists throughout the entire life of the program, and is usually used to store things like global variables, or variables created with the static clause. For example:
int somenumber = 5;
On many systems this variable uses 4 bytes of memory. This memory can come from one of two places. If a variable is declared outside of a function, it is considered global, meaning it is accessible anywhere in the program. Global variables are static,
static int somenumber = 5;
The stack segment is near the top of memory with high address
Every time a function is called, the machine allocates some stack memory for it.
The allocation and deallocation for stack memory is automatically done
Stores variables used on the inside of a function (including the main()
function).
It’s a LIFO
, “Last-In,-First-Out”, structure. Every time a function declares a new variable it is “pushed” onto the stack.
The stack is managed by the CPU, there is no ability
to modify it
automatically
upper bound
grows and shrinks
as variables are created and destroyedwhilst
the function
that created them exists
A stack overflow occurs if the call stack pointer exceeds the stack bound
. The call stack may consist of a limited amount of address space, often determined at the start of the program.
The heap is the diametrical opposite of the stack
.
The heap is managed
by the programmer
, the ability to modify it is somewhat boundless
The heap is large, and is usually limited
by the physical memory
available in an embedded environment and in a PC it is stored within paging files on main memory (SSD)
This is memory that is not automatically managed – you have to explicitly allocate (using functions such as malloc()
,calloc()
,realloc()
), and deallocate (free()
) the memory.
The heap requires pointers to access it
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int y = 4; char *str;
printf("stack memory: %d\n", y);
str = malloc(100*sizeof(char));
str[0] = 'm';
for(int i =0; i< 100; i++)
{
printf("heap memory: %c\n", str[i]);
}
free(str);
printf("heap memory: %c\n", str[0]);
return 0;
}
a character, 1 byte of memory which is:
an integer or a float, 4 byte of memory which is:
a double value, 8 byte of memory which is :
&
(Ampersand) and *
(Asterisk) symbol.&
we can reference the variable’s address when used with itself. *
is a variable that stores the address of another variable. int main ()
{ // The variable has its own address (unknown to us now)
int n = 11;
// this variable stores the address of the other variable
int *ptrToN = n;
printf(“n’s address: %d and %d ptrToN value \n”, &n, ptrToN);
printf(“n’s value: %d and ptrToN points to value %d \n”, n, *ptrToN);
return 0;
}
n’s address: 0x7fff20494e4c and 0x7fff20494e4c ptrToN value
n’s value: 11 and ptrToN points to value 11
int main ()
{
int n = 11, i;
char ptr[11] = "hello world";
for (i = 0; i < n; i++)
{
printf ("\t%p || ptr[%d] = %c\n", &ptr[i],i,ptr[i]);
}
printf("\t%p || ptr[] = %c \n", &ptr,*ptr);
return 0;
}
providing formative feedback for Assignment-1-Part-1
## General Memory Layout ![bg right:50% 80%](../../figures/stackVsHeap.png) - **Machine Code**: stores the code being executed >> - **stack**: stores local variables (static nature) >> - **heap**: dynamic memory for programmer to allocate >>