×

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

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

Difference between Pre-increment and Post-increment in C

Increment operators are the kind of operators that are used to increment the cost of the operand by way of 1. In different words, the increment operator is an operator...

4 minutes read.

Functions in C

What is function? Functions are defined as the set of announcements which take inputs, perform operations and provide results. The operation of a function only performs when the function gets called....

4 minutes read.

Keywords in C

A keyword is a word that has a predetermined meaning. The C compiler knows the purposes of the keywords. The objectives of these keywords cannot be modified. Keywords are the...

9 minutes read.

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

Java Array vs ArrayList

As we all know, arrays are linear data structures that allow you to add elements to them continuously in memory address space, whereas ArrayList is a Collection framework class. Despite...

4 minutes read.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 minutes read.

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.

How to create a binary file in C?

Creating a binary file in C language is very simple, requiring only some specific functions to be implemented. There are also other binary modes: read, and write, which will be...

7 minutes read.

Use of Structure in C

We can usually use arrays in C programming to store elements of the same data type. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and...

3 minutes read.

strcat() Function in C

Strings in c: A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The...

3 minutes read.

Long int in C

We use data type to avoid the type of data to be stored for a variable at the time of declaration.So, we can say that the variable type is known...

3 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.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

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.

GCD of Two Numbers in C

The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that...

3 minutes read.

Difference between while and do-while loop in C

Introduction Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes. Both loops process the program/code in the...

3 minutes read.