Month End Sale: Get Extra 10% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now

Memory Management in C

Level : Intermediate
Mentor: Shailendra Chauhan
Duration : 00:02:00

Memory Management in C

The process of assigning computer programs to physical or virtual memory space. The allocation might be done before or during program implementation.

Static Memory Allocation

During compilation, the compiler allocates a certain amount of RAM. The Operating System uses a data structure called Stack to manage memory. Exact memory requirements must be determined in advance, as memory allocation cannot be modified.

Dynamic Memory Allocation

The system memory is handled during runtime. The dynamic memory management operations include malloc(), calloc(), realloc(), and free(). The C standard library header file (<stdlib.h>) defines these four functions. It consumes the system memory's heap space. 

Functions for dynamic memory allocation in C

In the C language, there are four functions for dynamic memory allocation:

  1. Malloc() function in C.
  2. Callloc() function in C
  3. Realloc() function in C
  4. Free() function in C

malloc() function

A single block of memory is allocated according to the user-specified size. It returns null if the RAM is insufficient. It's important to remember that malloc() returns a reference to the start of the freshly created memory block, and it's up to the programmer to free this memory when it's no longer required.

calloc() function

It not only reserves the desired amount of RAM but also sets the bytes to 0. It allocates several blocks of memory. If there is insufficient memory, it returns null. It's essential to remember that calloc() returns a reference to the start of the newly allocated memory block, and it's up to the programmer to free this memory when it's no longer required.

realloc() function

This enables a programmer to dynamically alter the extent of previously allocated memory, making it an effective tool for memory optimization. The realloc() function allocates a new block of memory of the specified size, copies the contents of the old block into the new one, and then frees the old block.

free() function

C's free() function is a powerful tool that allows programmers to release previously allocated dynamic memory. This function is particularly useful for memory management in large C-based systems. Programmers can use the free() function to reallocate memory to different functions as needed, without worrying about overwriting vital data or triggering system problems.



Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this