what is a inbuilt library function to adjust the allocation dynamic memory size?
Answers
Explanation:
Search
BINARYUPDATES
Dynamic Memory Allocation in C Programming
In all of our previous lessons, we have used static memory allocation in our programs. In this lesson, we will learn about Dynamic Memory Allocation in C Programming. Let’s first understand difference between Static Memory Allocation and Dynamic Memory Allocation. In Static Memory Allocation user requested memory will be allocated at compile time. Whereas In Dynamic Memory Allocation, memory will be allocated while executing programs that means at run time. The Dynamic Memory Allocation allows us to modify memory size at run time.
Table of Contents [hide]
1 Dynamic Memory Allocation in C Programming
2 malloc() – Allocate Block of Memory
3 calloc() – Allocate Block of Memory
4 realloc() – Reallocate Block of Memory
5 free() – Frees Block of Memory
Dynamic Memory Allocation in C Programming
Dynamic Memory Allocation is unique feature of C Programming Language. This allows us to create data types and structures of any size and length which suits our program. There are two common application of dynamic memory allocation, these are while using dynamic arrays and dynamic data structure e.g. linked lists in C Programs. Let’s see use case. There comes a situation in programming, when we don’t know exact size of array until compiler compiles the code and generate executable. The size of array we have declared maybe not enough or more than required. In this case use of dynamic memory allocation is best solution. Now the question is how to allocate dynamic memory? The answer is C Language supports 4 library functions which are knows as memory management functions (malloc, calloc, realloc, free). These functions are defined in #include<stdlib.h> header file. We will see each function in detail. Let’s first understand organization of application memory. The heap is a part of application memory which will be used while allocating and de-allocating memory using memory management functions. The Heap is also known as Free Memory. The figure below will illustrate.
Application Memory Organization
Application Memory Organization
The Heap memory is used for dynamic memory allocation during execution of the program. The size of heap memory keeps changing. As mentioned earlier heap is free pool of memory. This is enough as an introduction. The list of dynamic memory management functions.
Functions Syntax Description
malloc void* malloc(size_t size) Allocates the specified number of bytes user requested.
calloc void* calloc(size_t num, size_t size) Allocates the specified number of bytes and initialize them to zero
realloc void* realloc(void* ptr, size_t size) Modify the previously allocated memory block (increase or decrease memory size and reallocate)
free – Release previously allocated memory block
malloc() – Allocate Block of Memory
“malloc()” the name itself states “memory allocation”. The malloc() function allocate space in memory during run time of the program. Malloc() function does not not initialize memory space allocated at run time. If we try to access this memory, we will receive garbage value. Malloc() returns NULL Pointer, in case if it could not able to allocate requested amount of memory.