Suppose we have an array of length 5 named a shown below
Now during the length of the array (5) is fixed here. But what if we need to change the size of the array as per circumstances? It won’t be possible right?
Here comes the idea of dynamic memory allocation where we can change the size of the array shown above as per our need and whenever we need.
In case of static memory allocation there are some drawbacks like-wastage of memory, less flexibility, permanent allocation of variables etc. To overcome these situations the concept of dynamic memory allocation has been introduced. In dynamic memory allocation the memories can be allocated dynamically at run time.
So we can say that, The procedure of memory allocation in which the size of some particular data structures can be changed in runtime, is called Dynamic Memory Allocation (DMA).
For using dynamic memory allocation in C language, we need to include the standard library header file (stdlib.h) in our code.
Advantages of DMA?
1. The variables are allocated until the program unit gets inactive.
2. Unlike static memory allocation, memory allocation can be done during program execution (runtime).
3. It is more efficient than static memory allocation.
4. There is a flexibility in reusing the memory and memory can also be freed when required.
There 4 library functions that are described under stdlib.h:
Let’s read about them minutely.
1. malloc(): To allocate memory dynamically we use the function which is called as malloc() function. malloc() function on success returns the base address of the allocated memory on failure it returns the null value…(Read more in details)
2. calloc(): To allocate memory dynamically we also use the function which is called as calloc() function. calloc() function on success returns the base address of the array and every memory location will be initialized to 0…(Read more in details)
3. realloc(): At the time of memory allocation using calloc() or malloc(), it may be possible memory is excess or insufficient. So for that reason we need to reallocate memory at the run time…(Read more in details)
4. free(): The free() method in C is used to dynamically de-allocate the memory. It helps to reduce wastage of memory by freeing it…(Read more in details)
- Elementor #14055
- HPE Off Campus Recruitment Drive | Software Engineer | 2022 2021 | Codewindow.in
- Yash Technologies Hiring | Trainee Programmer-Java | Codewindow.in
- Trianz Off Campus Hiring Drive | Graduate Engineer trainee | Codewindow,in
- Remainder mod 11 | Accenture Solve
- Non Duplicate String | Accenture Solve
- Autobiography Number Solution | Codewindow.in
- Count kth Digit Solution | Codewindow.in
- Revature Interview Experience | Part 1 | Codewindow.in
- Persistent Interview Experience | Codewindow.in
- L&T Off Campus Drive 2021 | Senior Engineer | 2019/ 2020/2021 | Codewindow.in
- Cognizant GenC Recruitment Drive | Programmer Trainee | Codewindow.in
- Amdocs Off Campus Hiring | System Analyst | Codewindow.in
- Zoho Off Campus Hiring | Software Developer | 2018/ 2019/ 2020/2021 | Codewindow.in
- Kumaran Systems Off Campus Hiring | Trainee Programmer | 2020 2019 | Codewindow.in
- Accenture ASE Interview Experience
- CodeLogicX Recruitment Drive | Software Developer Trainee | 2021, 2021 | Codewindow.in
- Accenture SE Interview Experience
- Juspay Hiring Challenge | Think big with Functional Programming | Codewindow.in
- Exam Answers
- IBM Off Campus Hiring | Associate System Engineer | Codewindow.in
- FindMax Problem
- Walmart Off Campus Hiring | Walmart CodeHers | Codewindow.in
- Quadratic Equation Solution | Codewindow.in
- Accenture Solution | Code in Marathon | Codewindow.in
- Amazon Off Campus Internship Drive | Amazon WoW for the batch of 2022, 2023, and 2024 | Codewindow.in
- Binary Tree Combination
- Sum of all the prime divisors of a number
- Mindtree Interview Questions and Experience Part 1 | Codewindow.in
- Apisero Off Campus Recruitment Drive | Software Engineer | 2021 2020 2019 2018 | Codewindow.in
- Infosys Mega Off Campus HIring | Technincal Process Executive | Codewindow.in
- Amazon Off Campus Hiring | Software Development Engineer | 2021/2020/2019/2018 | Codewindow.in
- Coding Questions (79)
- HR Questions (4)
- Interview Experience (11)
- Job Info (143)
- Miscellaneous (43)
- Programming in C (56)
- Programming in C++ (6)
- Programming in JAVA (11)
- Programming in Python (126)
- Recruiting Companies (6)
- TCS NQT (23)
- Uncategorized (43)
- Wipro NLTH (8)
- WIpro NLTH Coding Solve (1)