- C Programming Basics
- C Tutorial
- C Program Structure
- C Basic Syntax
- C Data Types
- C Constants
- C Variables
- C Operators
- C Ternary Operator
- C Storage Classes
- C Flow of Control
- C Decision Making
- C if if-else Statement
- C switch Statement
- C Loops
- C for Loop
- C while Loop
- C do-while Loop
- C goto Statement
- C break Statement
- C continue Statement
- C Popular Topics
- C Arrays
- C Strings
- C Pointers
- C Functions
- C Recursion
- C Scope Rules
- C Programming Advance
- C Structures
- C Unions
- C Bit Fields
- C Enumerations
- C Input & Output
- C Typedef
- C Preprocessors
- C Type Casting
- C Recursion
- C Error Handling
- C Linked Lists
- C Stacks
- C Queues
- C Binary Trees
- C Header Files
- C File I/O
- C Variable Arguments
- C Memory Management
- C Command Line Arguments
- C Programming Examples
- C Programming Examples
- C Programming Test
- C Programming Test
C Memory Management
C language provides several functions for memory allocation and management. These functions can be found in the header file <stdlib.h>.
Dynamic Allocation Functions in C
Here this table lists several functions available in C to perform memory allocation and management
Function | Description |
---|---|
void *calloc(int num, int size) | used to allocate an array of num elements each of which size in bytes will be size |
void *malloc(int num) | used to allocate an array of num bytes and leave them initialized. |
void free(void *address) | This function release a block of memory block specified by address. |
void *realloc(void *address, int newsize) | used to re-allocate memory extending it upto newsize. |
C Dynamic Memory Allocation Example
Here is an example of dynamic memory allocation in C:
/* C Memory Management - This program illustrates * the concept of dynamic memory allocation in C */ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> void main() { char name[100]; char *desc; int len1, len2; clrscr(); printf("Enter your name : "); gets(name); desc = malloc(200 * sizeof(char)); if(desc == NULL) { fprintf(stderr, "Error - unable to allocate required memory\n"); } else { strcpy(desc, "a computer programmer."); } printf("Name = %s\n", name ); printf("Description: %s, %s\n", name, desc); len1 = strlen(desc); len2 = strlen("a computer programmer."); if(len1 == len2) { printf("\nDynamically allocated desc's length = %d\n", len1); } else { printf("Not possible..!!"); } getch(); }
Here is the sample run of this C program:

Same program can be written using calloc(), the only thing you need to replace malloc() with calloc() as follows:
calloc(200, sizeof(char));
Here is another program, showing how to resize and release memory in C:
/* C Memory Management - Dynamic Memory Management Program */ #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<string.h> void main() { char name[100]; char *desc; clrscr(); printf("Enter your name : "); gets(name); desc = malloc(30 * sizeof(char)); if(desc == NULL) { fprintf(stderr, "Error - unable in allocating the required memory\n"); } else { strcpy(desc, "a C programmer."); } desc = realloc(desc, 100 * sizeof(char)); if(desc == NULL) { fprintf(stderr, "Error - unable to allocate required memory\n"); } else { strcat(desc, "\nHe is getting strong idea about C Memory Management."); } printf("Name = %s\n", name ); printf("Description: %s, %s\n", name, desc); free(desc); getch(); }
Here is the sample output of this C program:

On trying the above example without re-allocating extra memory and strcat() function will give an error due to lack of available memory in description
« Previous Tutorial Next Tutorial »
Follow/Like Us on Facebook
Subscribe Us on YouTube