×

Dynamic memory allocation

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file.
Function Syntax
malloc() malloc (number *sizeof(int));
calloc() calloc (number, sizeof(int));
realloc() realloc (pointer_name, number * sizeof(int));
free() free (pointer_name);
malloc() function The process of allocating memory during program execution is called dynamic memory allocation. It returns NULL if the memory is not sufficient.  It does not initialize the memory allocation during the execution. Syntax:
malloc (number *sizeof(int));
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
 char *ma;
 /* Dynamically memory allocated */
 ma = malloc( 20 * sizeof(char) );
 if(ma== NULL ){
 printf("Can not allocate requested memory\n");
 }
 else{
 strcpy( ma,"C Tutorial");
 }
 printf("Dynamically allocated memory : %s\n", ma );
 free(ma);
}
Output
Dynamically allocated memory : C Tutorial
calloc() function in c The calloc() function is used to allocate the multiple blocks of request memory.   It allocates the memory with zero-initialize. Syntax:
calloc (number, sizeof(int));
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
char *ma;
/* memory is allocated dynamically */
ma = calloc( 20, sizeof(char) );
if( ma== NULL ){
printf("Can not allocate requested memory \n");
}
else{
strcpy( ma,"C Example");
}
printf("Dynamically allocated memory%s:\n ", ma);
free(ma);
}
Output
Dynamically allocated memoryC Example:
realloc() function in c The realloc() function is used to modify the allocated memory size by malloc and calloc functions to new size. Synatx:
realloc (pointer_name, number * sizeof(int));
free() function in c The free() function is used to free the allocated memory by malloc(), calloc() functions and return the memory of the system. syntax:
free (pointer_name);
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
char *ma;
ma = malloc( 20 * sizeof(char) ); /* memory allocated dynamically */
if( ma == NULL ){
printf("Couldn't able to allocate requested memory\n");
}
else{
strcpy(ma,"C language");
}
printf("Dynamically allocated memory %s:", ma);
ma=realloc(ma,100*sizeof(char));
if( ma == NULL){
printf("Couldn't allocate requested memory");
}
else{
strcpy(ma,"space extended upto 100 characters");
}
printf("Resized memory : %s\n",ma);
free(ma);
}
Output
Dynamically allocated memory C language:Resized memory : space extended 
upto 100 characters

Related Topics

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.

What is String Comparison in C

String comparison is the process of comparing two strings (sequences of characters) to determine if they are equal, or if one is greater or less than the other. The comparison...

4 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

C Program to Find the Largest Number using Ternary Operator

In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C. Ternary Operator: The ternary...

3 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.

Float in islower() in C

Introduction: This article briefly discusses the float in islower() in C. The islower() function checks if the input character passed inside the function is lowercase. Lowercase letters include (a-z). The islower()...

4 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

Length of an Array Function in C

In C, there is no built-in function to get the length of an array. However, there are a few ways you can determine the length of an array. It is necessary...

15 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.

Ceil function in C

Introduction In the C Programming Language, the ceil function is a library function which is used to obtain the ceil value, i.e., it returns the smallest integer that is greater than...

4 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.

Addition of Matrix in C

The mathematical operation of includingor greater matrices is called the addition of matrices. A matrix is a square series of numbers, symbols, words, letters, and different things. In this article,...

3 minutes read.