In this lesson, you will learn about memory management in C, static and dynamic allocations, and examples to better understand the topics.
Memory management is handling and coordinating a computer’s main memory. Any variable function syntax in a programming language occupies some space in memory. If one variable occupies space in
a computer, no other variable can occupy it. So, it is the aspect of any programming language to make the program efficient by providing clever memory management techniques. Memory management is one of the aspects of any programming language.
There are two types of momery allocation in C Programming:
Static means not changeable. If in programming, any memory is allocated at compilation time, and it remains the same throughout the program execution. This kind of memory allocation address or size of the memory remains the same.
Dynamic allocation is can be changed at run time. In programming, any memory is allocated at run time, and increases or decreases as per programming demands, so it is considered dynamic programming.
It is a great advantage in dynamic memory allocation that memory can be handled efficiently in this way.
picture on page 69
Let’s, suppose you have to write a program for enrolling 100 students, it is easy to define an array of
100 students. You can select 2- or 3-dimensional array to store student names, subjects, and scores. 100
students, each student can have 10 subjects and each subject can have 2 types of scores (mid and final
term marks).
char Students[100][10][2];
However, again think of a situation when you are unaware of the number of students going to be enrolled in the university. In this case, you have to think of some dynamic memory allocation techniques like defining a pointer.
the malloc is a function in the C library that allocates memory and returns a pointer to it.
pointer = (Typecasting*) malloc(size);
Example
pointer = (float*) malloc(200 * sizeof(float));
The pointer has 800 bytes of memory because the float byte size is 4, pointer holds the address of the first byte in the allocated memory.
Below is an example of how to use the dynamic memory allocation malloc(), and the dynamic releaseĀ allocated memory function free():
// Program calculate the total marks of all the subjects entered by the student #include <stdio.h> #include <stdlib.h> int main () { int noOfElements, sub, *ptr, total_marks = 0; printf ("Enter number of student marks: "); scanf ("%d", &noOfElements); //malloc dynamically allocates the memory ptr = (int *) malloc (noOfElements * sizeof (int)); if (ptr == NULL) { printf ("exit! no memory is allocated."); exit (0); } printf ("Please Enter the Subject marks:\n"); for (sub = 0; sub < noOfElements; ++sub) { scanf ("%d", ptr + sub); total_marks += *(ptr + sub); } printf ("Total Marks = %d", total_marks); // release the memory free (ptr); return 0; }
Output
Enter number of student marks: 2 Please Enter the Subject marks: 2 2 Total Marks = 4
The calloc function is a function in the C library that allocates memory and returns a pointer to it. It’s similar to the malloc() function, however, the malloc() function does not set the memory to zero, while the calloc function does set allocated memory to zero.
pointer = (Typecasting *) calloc (n, size);
Example
pointer = (float *) malloc (25, sizeof (float));
The pointer has contiguous space in memory for 25 elements of float type.
Example of dynamic memory allocation calloc() and the dynamic release allocated memory function free():
// Program calculate the total marks of all the subjects entered by the student #include <stdio.h> #include <stdlib.h> int main () { int noOfElements, sub, *ptr, total_marks = 0; printf ("Enter number of student marks: "); scanf ("%d", &noOfElements); //malloc dynamically allocates the memory ptr = (int *) calloc (noOfElements, sizeof (int)); if (ptr == NULL) { printf ("exit! no memory is allocated."); exit (0); } printf ("Please Enter the Subject marks:\n"); for (sub = 0; sub < noOfElements; ++sub) { scanf ("%d", ptr + sub); total_marks += *(ptr + sub); } printf ("Total Marks = %d", total_marks); // release the memoryfree(ptr); return 0; }
Output
Enter number of student marks: 2 Please Enter the Subject marks: 2 2 Total Marks = 4