About Me

Hello there! Before talking about qualifications and professional abilities, I would like to introduce myself. I am a Yuvraj Basnet and professional software developer and have been working in this field for 3 long years. I have finished my graduation from The University of Delhi on Computer Science Engineering I considered myself a reasonably experienced programmer with a particular interest in the human side of software development, computers are fascinating machine, but they're mostly reflection of the people using them. In the art of software development, studying code isn't enough; you have to study the people behind the software, too.

Dynamic Memory Allocation In C Programming

Dynamic Memory Allocation In C Programming

Dynamic Memory Allocation In C Programming

Hi there! In the previous article we discussed some Advanced level Interview Q&A now in this article we will discuss some important Dynamic Memory Allocation In C Programming which is a very important topic in C.
So, let's get started

Dynamic Memory Allocation:

Dynamic Memory Allocation performs manual allocation and freeing of memory according to your programming needs for Dynamic Memory Allocation in the C programming along a group of line of codes i.e, Function in the C standard library, and C uses functions which are malloc(), realloc(), calloc() and free().

Dynamic memory is maintain and served with pointers that point to the newly allocated memory space in an area which we call the heap. Using this we can create and destroy an array of elements at runtime without any errors. It means the size of allocated memory can shrink at the program runtime when needed. So we can say that Dynamic Memory Allocation can be defined as a procedure in which the size of a data structure (like Array) can be changed during the runtime.

While coding, if you know 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 105 characters, so you can define something as follows −
char name[105];



1. Malloc() [Memory Allocation]

Allocates the memory of requested size and returns the pointer to the primary byte of allocated space. The pointer returned is of void type which can be casted into a pointer of any form. It returns garbage initialized memory.
If the available space is insufficient, allocation fails and malloc() returns a NULL pointer.

Syntax: 
ptr = (cast-type*) malloc(byte-size);

Example-

OUTPUT 
Enter number of elements: 4
Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3, 4


2. calloc(): [contiguous allocation]
Allocates the space for elements of an array. Initializes the elements to zero and returns a pointer to the memory. It also returns void type memory that can be casted into appropriate type. If allocation fails null is returned.

Syntax:
    ptr = (datatype*) calloc(total_elements,size_of_each_element);

Example-

OUTPUT
Enter number of elements: 4
Memory successfully allocated using calloc.
The elements of the array are: 1, 2, 3, 4,


3. re-alloc: [Reallocation]
It is wont to modify the size of previously allocated memory space. It also returns void type, uninitialized memory that can be casted into appropriate type. If the new size is larger than old size the previously initialized
elements remains same. If allocation fails null is returned.

Syntax:
    ptr = (datatype*) realloc(ptr,new_total_size_in_bytes);

Example-


OUTPUT
Enter number of elements: 5
Memory successfully allocated using calloc.
The elements of the array are: 1, 2, 3, 4, 5,


Enter the new size of the array: 10
Memory successfully re-allocated using realloc.
The elements of the array are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,

4. free(): [De-allocation]
Frees or empties the previously allocated memory space. The memory allocated using functions malloc() and calloc() aren't de-allocated on their own. Hence the free() method is employed , whenever the dynamic memory allocation takes place. It helps to scale back wastage of memory by freeing it.

Syntax: free(ptr);

Example-

OUTPUT
Enter number of elements: 5
Memory successfully allocated using malloc.
Malloc Memory successfully freed.


Memory successfully allocated using calloc.
Calloc Memory successfully freed.














Post a Comment

0 Comments