Basic Memory Management in C Programming | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

This chapter explains dynamic memory management in C. The C programming language provides several functions for memory allocation and management. These functions can be found in the <stdlib.h> header file.

S.N.Function & Description
1

void *calloc(int num, int size);

This function allocates an array of num elements each of which size in bytes will be size.

2

void free(void *address);

This function releases a block of memory block specified by address.

3

void *malloc(int num);

This function allocates an array of num bytes and leave them uninitialized.

4

void *realloc(void *address, int newsize);

This function re-allocates memory extending it upto newsize.


Allocating Memory Dynamically

While programming, if you are aware of the size of an array, then it is easy and you can define it as an array. For example, to store a name of any person, it can go up to a maximum of 100 characters, so you can define something as follows −

char name[100];

But now let us consider a situation where you have no idea about the length of the text you need to store, for example, you want to store a detailed description about a topic. Here we need to define a pointer to character without defining how much memory is required and later, based on requirement, we can allocate memory as shown in the below example −

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {

char name[100];
char *description;

strcpy(name, "Zara Ali");

/* allocate memory dynamically */
description = malloc( 200 * sizeof(char) );

if( description == NULL ) {
fprintf(stderr, "Error - unable to allocate required memory
");
}
else {
strcpy( description, "Zara ali a DPS student in class 10th");
}

printf("Name = %s
", name );
printf("Description: %s
", description );
}

When the above code is compiled and executed, it produces the following result.

Name = Zara Ali
Description: Zara ali a DPS student in class 10th

Same program can be written using calloc(); only thing is you need to replace malloc with calloc as follows −

calloc(200, sizeof(char));

So you have complete control and you can pass any size value while allocating memory, unlike arrays where once the size defined, you cannot change it.

Resizing and Releasing Memory

When your program comes out, operating system automatically release all the memory allocated by your program but as a good practice when you are not in need of memory anymore then you should release that memory by calling the function free().

Alternatively, you can increase or decrease the size of an allocated memory block by calling the function realloc(). Let us check the above program once again and make use of realloc() and free() functions −

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {

char name[100];
char *description;

strcpy(name, "Zara Ali");

/* allocate memory dynamically */
description = malloc( 30 * sizeof(char) );

if( description == NULL ) {
fprintf(stderr, "Error - unable to allocate required memory
");
}
else {
strcpy( description, "Zara ali a DPS student.");
}

/* suppose you want to store bigger description */
description = realloc( description, 100 * sizeof(char) );

if( description == NULL ) {
fprintf(stderr, "Error - unable to allocate required memory
");
}
else {
strcat( description, "She is in class 10th");
}

printf("Name = %s
", name );
printf("Description: %s
", description );

/* release memory using free() function */
free(description);
}

When the above code is compiled and executed, it produces the following result.

Name = Zara Ali
Description: Zara ali a DPS student.She is in class 10th

You can try the above example without re-allocating extra memory, and strcat() function will give an error due to lack of available memory in description.

The document Basic Memory Management in C Programming | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Basic Memory Management in C Programming - Programming and Data Structures - Computer Science Engineering (CSE)

1. What is memory management in C programming?
Ans. Memory management in C programming refers to the process of allocating and deallocating memory during program execution. It involves managing the computer's primary memory to optimize the usage of available memory resources.
2. How is memory allocated in C programming?
Ans. Memory in C programming can be allocated dynamically using functions like malloc(), calloc(), and realloc(). These functions allow the programmer to request a specific amount of memory from the operating system at runtime.
3. What is the difference between malloc() and calloc() functions?
Ans. The malloc() function in C is used to allocate a block of memory of a specified size, while the calloc() function is used to allocate a block of memory of a specified size and initializes all its bits to zero. In other words, calloc() is similar to malloc() but additionally initializes the allocated memory to zero.
4. How is memory deallocated in C programming?
Ans. Memory allocated dynamically using malloc(), calloc(), or realloc() should be deallocated using the free() function. The free() function releases the memory back to the operating system, allowing it to be reused for other purposes.
5. What is memory fragmentation and how does it affect memory management in C programming?
Ans. Memory fragmentation occurs when free memory blocks become scattered throughout the memory space, resulting in inefficient memory utilization. It can lead to memory leaks and reduced performance. Memory management techniques like compaction and memory pooling are used to minimize memory fragmentation and optimize memory usage in C programming.
119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

study material

,

video lectures

,

ppt

,

Summary

,

shortcuts and tricks

,

MCQs

,

Previous Year Questions with Solutions

,

Basic Memory Management in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

,

Basic Memory Management in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

,

pdf

,

Objective type Questions

,

Basic Memory Management in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

,

Extra Questions

,

Semester Notes

,

Important questions

,

Viva Questions

,

Exam

,

past year papers

,

Sample Paper

,

practice quizzes

,

mock tests for examination

,

Free

;