Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Data Structure » Dynamic memory allocation in C using malloc, realloc, calloc and free

  • Fundamental Duties of Indian Citizens
    Fundamental Duties of Indian Citizens | 11 मौलिक कर्तव्य हिंदी में General Knowledge
  • Holi kyon manate hain in hindi? | Festival of colors in hindi
    Holi kyon manate hain in hindi? | Festival of colors in hindi Festival
  • International Labour's Day in Hindi | अंतर्राष्ट्रीय मजदूर दिवस 1 मई
    International Labour’s Day in Hindi | अंतर्राष्ट्रीय मजदूर दिवस 1 मई General Knowledge
  • Lanka Kand Summary in Hindi | Ram Vs Ravana | लंका काण्ड
    Lanka Kand Summary in Hindi | Ram Vs Ravana | लंका काण्ड Spiritual
  • World Environment Day in Hindi : Objective, Importance, Theme
    World Environment Day in Hindi : Objective, Importance, Theme General Knowledge
  • Diwali The Festival of Lights
    Diwali 2022, Indian Festival of Lights essay (दिवाली त्योहार पर निबंध, कहानी) Festival
  • Vat Savitri Vrat in Hindi, Vat Savitri Puja | वट सावित्री पूजा
    Vat Savitri Vrat in Hindi, Vat Savitri Puja | वट सावित्री पूजा Festival
  • Republic day गणतंत्र दिवस | Happy Republic Day
    Republic day गणतंत्र दिवस कब और क्यों मनाया जाता है? Festival

Dynamic memory allocation in C using malloc, realloc, calloc and free

Posted on April 18, 2022September 12, 2022 By GeekCer Education No Comments on Dynamic memory allocation in C using malloc, realloc, calloc and free
Dynamic memory allocation in C, malloc, realloc, calloc and free

The C programming technique of allocating necessary memory to hold data during runtime is known as dynamic memory allocation. We’re sure you’re familiar with arrays. When you want to process data and you know how big it is, an array is the ideal option.

Assume you’re reading data from a file or accepting keyboard input. You have no idea how large the data may be in such a case. In such a situation, an array is not useful.

C has the capability of dynamic memory allocation to deal with such situations. At runtime, you can allocate Memory for your storage. When you’ve finished your task, you can deallocate the memory.

In the c programming language, we handle dynamic memory allocation by following four library functions, and the dynamic memory allocation in the c header file is stdlib.h.

  • malloc()
  • realloc()
  • calloc()
  • free()

Table of Contents

  • Difference between Static and Dynamic Memory Allocation
  • Key Points to Remember About Dynamic Memory Allocation in C
  • Syntax of malloc() function in C
  • Example of malloc() function in C
  • Syntax of calloc() function in C
  • Example of calloc() function in C
  • Syntax of realloc() function in C
  • Syntax of free() function in C
  • Difference Between malloc() and calloc() with Examples

Difference between Static and Dynamic Memory Allocation

Following are the differences between Static and Dynamic memory allocation.
Static Vs Dynamic Memory Allocation

Static Memory AllocationDynamic Memory Allocation
Static memory allocation occurs when memory allocation is done at the time of compilation.Dynamic memory allocation occurs when memory allocation is done at the run time.
Static memory allocation is faster because it saves running time.Dynamic memory allocation is slower than static memory allocation.
We can not reuse the memory in case of Static Memory Allocation.In case of Dynamic memory allocation we can reuse the memory.
Memory allocation that is static is less efficient than memory allocation that is dynamic.When compared to static memory allocation, dynamic memory allocation is more efficient.
To manage static allocation of memory, it is use stack.To manage dynamic allocation of memory, it is use heap.

Also Read: Stack Vs Heap Memory

Key Points to Remember About Dynamic Memory Allocation in C

  • You can use malloc() method for allocating memory in terms of bytes.
  • When you use the malloc() method to allocate memory, it returns a reference (pointer) to the storage where the memory was allocated.
  • When all of your memory-related tasks are completed, use the free() function to deallocate memory.

Syntax of malloc() function in C

We specified the kind of data to be stored and the number of bytes required to store a single data in memory in the malloc() parameter.

base = (dataType*) malloc(size);
base = (int*) malloc(50 * sizeof(int));

Example of malloc() function in C


#include<stdio.h>
#include<stdlib.h>
#define MAX 3

int main() {
  int i;
  int * base;
  int total = 0;
  base = (int * ) malloc(MAX * sizeof(int));
  if (base == NULL) {
    printf("Unable to allocate memory");
    exit(0);
  }
  printf("Enter elements: ");
  for (i = 0; i < MAX; ++i) {
    scanf("%d", base + i);
    total += * (base + i);
  }
  printf("Total=%d", total);
  free(base);
  return 0;
}


Output:
Enter elements: 10
20
30
Total=60

Syntax of calloc() function in C

base = (dataType*) calloc(n, size);
base = (int*) calloc(10, sizeof(int));

Example of calloc() function in C


#include<stdio.h>
#include<stdlib.h>
#define MAX 4

int main() {
  int i;
  int * base;
  int total = 0;
  base = (int * ) calloc(MAX, sizeof(int));
  if (base == NULL) {
    printf("Unable to allocate memory for data");
    exit(0);
  }
  printf("Enter elements: ");
  for (i = 0; i < MAX; ++i) {
    scanf("%d", base + i);
    total += * (base + i);
  }
  printf("Total=%d", total);
  free(base);
  return 0;
}


Output:
Enter elements: 11
22
33
44
Total=110

Syntax of realloc() function in C

base = realloc(base, <new-size>);

Syntax of free() function in C

We use free() function to deallocate memory in C. You can use malloc() or calloc() function to allocate memory. The function itself does not free the memory. You have to call free() method explicitly to free the memory.

free(base);

Difference Between malloc() and calloc() with Examples

malloc()calloc()
The malloc() function creates a single block of the specified size provided by the user.The calloc() function creates multiple blocks of the specified size.
The malloc() function initializes the reserved memory block to a unexpected/garbage value.The calloc() function initializes the reserved memory block to zero.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Data Structure Tags:deallocate memory in c, Dynamic memory allocation in C header file, malloc in c, malloc vs calloc, Static memory allocation in C, What is dynamic memory allocation in C with example?

Post navigation

Previous Post: Linked List in Data Structure and Algorithm, Types, Operations

More Related Articles

Bubble Sort Algorithm in Data Structure | Bubble Sort Example Bubble Sort Algorithm in Data Structure | Bubble Sort Example Data Structure
Linear search algorithm | Linear search program in C, Example Linear search algorithm | Linear search program in C, Example Data Structure
Linked List in Data Structure and Algorithm, Types, Operations Linked List in Data Structure and Algorithm, Types, Operations C Language
Binary search in Data structures In C (Recursive and Iterative) Binary search algorithm in C, Recursive, Iterative, examples Data Structure

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved