Dynamic Memory Allocation

Why do we need dynamic memory When we create variable it uses static memory. The size uses before running the program. Sometimes we don’t even know what the size will be. And we can use dynamic memory. Dynamic memory is memory that is allocated after the program starts running. ...

Dynamic Memory uses

Create Lets create an integer array of 3 elements. Dynamic Memory behaves like an array, and its dataType specified by the type of pointer. int size = 3 * sizeof(*int); int *ptr = malloc(size); Now we can use 0,1,2 indexes to access the Memory; ptr[0] =5; // we can also use `*ptr` while default it point to 1st index(0) ptr[2] = 4; printf("%d \n", ptr[2]); We can also use calloc(3, sizeof(*ptr)) ...

Struct Dynamic Memory

We will learn how to store student struct with Dynamic memory. Dynamic Memory Struct #include <stdio.h> #include <stdlib.h> #include <string.h> struct Student { int id; char *name; }; int main(void) { struct Student *s = malloc(sizeof( struct Student)); if(!s){ // s == NULL perror("allocation failed"); return 1; } scanf("%d", &s->id); char temp[100]; scanf("%99s",temp); (*s).name = malloc(strlen(temp) + 1); if (!s->name){ perror("failed to allocate name"); free(s); return 1; } strcpy(s->name, temp); printf("\n%d\n", s->id); printf("Name: %s\n", s->name); free(s); return 0; } Student array #include <stdio.h> #include <stdlib.h> #include <string.h> struct Student { int id; char *name; }; int main(void) { struct Student *s = malloc(2 * sizeof(struct Student)); if (!s) { perror("allocation failed"); return 1; } for (int i = 0; i < 2; i++) { printf("Enter ID and name: "); scanf("%d", &s[i].id); char temp[100]; scanf("%99s", temp); s[i].name = malloc(strlen(temp) + 1); if (!s[i].name) { perror("failed to allocate name"); // free any previously allocated names before exiting for (int j = 0; j < i; j++) { free(s[j].name); } free(s); return 1; } strcpy(s[i].name, temp); printf("\nID: %d\n", s[i].id); printf("Name: %s\n", s[i].name); } // ✅ free all names after use for (int i = 0; i < 2; i++) { free(s[i].name); } free(s); return 0; } Simplify with function #include <stdio.h> #include <stdlib.h> #include <string.h> struct Student { int id; char *name; }; void freeStudent(struct Student *s, int counter){ for(int i =0; i<counter; i++){ free(s[i].name); } free(s); } int main(void) { int N = 2; struct Student *s = malloc(N*sizeof( struct Student)); if(!s){ // s == NULL perror("allocation failed"); return 1; } for (int i=0; i< N; i++) { printf("Enter id:"); scanf("%d", &s[i].id); char temp[100]; printf("Enter name: "); scanf("%99s",temp); s[i].name = malloc(strlen(temp) + 1); if (!s[i].name){ perror("failed to allocate name"); freeStudent(s,i); return 1; } strcpy(s[i].name, temp); printf("\n%d\n", s[i].id); printf("Name: %s\n\n", s[i].name); } freeStudent(s,N); return 0; }

Memor leaks

Memory leak happens when create dynamic Memory but doesn’t release :) Means when we don’t use free . int *ptr = malloc(N); // and we don't use `free(ftr)` function Memory Leak A common mistake of Memory leak happens when we create dynamic Memory inside function and forget to free it. void func(){ int *ptr; ptr = malloc(sizeof(*ptr)); /// ..... rest code /// we should free ptr end of its uses. } Reallocation Memory leak We can often forget to free old pointer and set null which is better practice. ...