Memory Management on C programming
Hi, today I will write about memory management in the C programming language. If you are a programmer you must be familiar with C right, because at school / university C is a mandatory lesson for a CS degree. C is the mother of everything, even Java uses C as a JVM, linux is made of C and so on. Learning C is good to get basic knowledge of programming and how computers work in general.
List of requirement :
- C installation on windows : https://cygwin.com/install.html
- vscode text editor : https://code.visualstudio.com/docs/setup/windows
Talking about RAM or memory, in general memory is like excel that consists of rows and columns, and each cell has an address.
Address of the highlighted cell is B4 (column B row 4), and the cell contains value ‘X’.
Illustration Memory Management on C
Illustration of memory allocation on C programming language, the lowest address is 0 Hexadecimal and the highest address lets say FFFFFF.
Stack :
This is the section of memory used for local variable storage. Every time you call a function, all of the function’s local variables get created on the stack. It’s called the stack because it’s like a stack of plates: variables get added to the stack when you enter a function, and get taken off the stack when you leave. Weird thing is, the stack actually works upside down. It starts at the top of memory and grows downward. example :
int main(){
char ch = 'A';
printf("char : %c\n", ch );
return 0;
}
Heap:
The heap is for dynamic memory: pieces of data that get created when the program is running and then hang around a long time. we can use malloc() to request space on heap memory.
int main(){
int *ptr;
ptr = malloc(10 * sizeof(*ptr)); //allocate 10 memory address allocation of int
if (ptr != NULL) {
*(ptr) = 10;
printf("value of ptr address = %d \n",*(ptr));
*(ptr+1) = 11;
printf("value of ptr address + 1 = %d \n",*(ptr + 1));
}
return 0;
}
Globals:
A global variable is a variable that lives outside all of the functions and is visible to all of them. Globals get created when the program first runs, and you can update them freely.
/* global variable declaration */
int globalVar;
int main(){
globalVar = 10;
printf("globalVar : %d\n", globalVar );
return 0;
}
Constants:
Constants are also created when the program first runs, but they are stored in read-only memory. Constants are things like string literals that you will need when the program is running, but you’ll never want them to change.
const int CONSTANT_VAR=6;
int main(){
printf("CONSTANT_VAR : %d\n", CONSTANT_VAR );
return 0;
}
Code:
Finally, the code segment. A lot of operating systems place the code right down in the lowest memory addresses. The code segment is also read-only. This is the part of the memory where the actual assembled code gets loaded.
How C data type allocate memory
For example, C integer data type has 4 byte range, and can hold values -2,147,483,648 to 2,147,483,647.
4 byte = 32 bit.
For example
int varInt = 7;
means the program will request 4 byte memory allocation to hold value 7. 7 in binary = 111. Because the memory allocation is 32 bit, 7 will have many leading zero:
00000000000000000000000000000111.
Source : Head First C — David Griffiths, Dawn Griffiths.