What is Dynamic Memory Allocation

Dynamic Memory Allocation

As the world of technology exponentially grows, there are more and more programming languages and their unique features being discovered every day. Because of this, there is a profound effect on the abilities of programmers such as being efficient with memory allocation that can speed up your program and having an Object-Oriented Programming paradigm in certain programming languages that offers an effective and secure structure for a program. We will discuss Dynamic Memory Allocation.

How does a program create a variable? 

Interestingly, we think that when we are programming, we are creating everything from scratch. That’s not exactly true. For instance, if we are creating any variable, it will be allocated to the memory that already exists in the computer instead of somehow creating a new memory to allocate our variable. In other words, computers already have pre-existing memory that is simply allocated to any variable that programmers will create in the program.   

What is Static Memory Allocation?

As everyone knows, we humans use programming languages to communicate with computers. However, there is a specific code of conduct that we must comply with in order to convey our purpose to the computer. For example, we create variables to let the compiler know that we would like to have memory allocated to x,y, and z variables. Because they are important to us, and they will be used when we compile the program later. So, you see here the memory is allocated prior to compiling the program. This is called “Static Memory Allocation”. In other words, Static Memory Allocation doesn’t allocate the memory while compiling the program. Clearly, a question might be popping up in your head: why would we need to have memory being allocated to the program while compiling? Why not allocate before compiling like Static Memory Allocation?

What is Dynamic Memory Allocation? Why Use it?

Briefly, Dynamic Memory Allocation is the process of allocating the memory while compiling instead of doing it prior to compiling the program as Static Memory Allocation does. 

We would like to assign memory to variables while compiling for a number of reasons. Firstly, we have to understand the premise behind static memory allocation. For instance, in static memory allocation, sometimes we would have to allocate the memory for the variables that have the unknown input, we don’t know what the input for x variable will be when the program compiles. Hence, we can allocate more (more could lead to inefficient memory usage) or less (less could lead to a program crash or misbehave) memory to such variables. Meanwhile, with dynamic memory allocation, there is no need to worry about allocating more or less memory to the variables. Because the memory will be allocated while the program compiles depending on the size and kind of input it gets. Overall improving memory efficiency, making the program faster and more productive. 

Types of Available memories:

There are two types of memories that are available for the programmers: Stack and Heap. 

★Stack is the memory where everything is well-defined when it comes to allocating or deallocating the memory. Hence, anything that takes place in the stack is well-structured. Stack memory can be accessed by both Static Memory Allocation and Dynamic Memory Allocation. 

★Heap is the aspect of the memory where there isn’t any order for allocating or deallocating the memory. Heap can only be accessed by Dynamic Memory Allocation. Importantly, programmers also need to deallocate the memory by using syntax: [delete pointerName; ]. Because memory is not deallocated by the compiler in the Heap memory.

Advantages of Dynamic Memory Allocation:

▸Improves Memory-space efficiency – Dynamic memory allocation comes with the ability to assign the right amount of memory to the right data depending upon the size and what kind of data it is. Interestingly, improving space efficiency by a decent amount, which is something that you wouldn’t find in static memory allocation.

▸Speed – As stated earlier, there is no need to assign memory to the variables beforehand, implying that a program won’t be using more or less memory, which leads to quicker programs.

▸Fewer chances of the program crashing or misbehaving – When there is no inappropriate allocation of memory to the variable, the program is less likely to crash or misbehave. 

▸More control in the programmer’s hands – In Static Memory Allocation, the ability to be efficient and productive somewhat relies on the program. Surprisingly, a program could be 100% right with the allocation of the memory and still a program could crash. Especially, when the input is unknown or unexpected to the program. Meanwhile, dynamic memory allocation provides more control in the hands of programmers. The ability to be productive and effective with memory efficiency is in the hand of a programmer.

Disadvantages of Dynamic Memory Allocation:

▸Hard to learn programming language – In order to program Dynamic Memory Allocation, a programmer would have to understand the use of pointers in C++. Furthermore, a programmer needs to be quite expert in order to effectively and efficiently allocate the memory. 

▸Coding speed might be slow – Dynamic Memory Allocation requires a lot of extra code as compared to the Static Memory Allocation. Unfortunately, this could lower the speed of a programmer. 

Conclusion 

In conclusion, Dynamic Memory Allocation allocates memory while compiling. Whereas, Static Memory Allocation allocates before compiling. Dynamic Memory Allocation is much more memory efficient than Dynamic Memory Allocation.

1 thought on “What is Dynamic Memory Allocation? (C++)”

  1. Pingback: Binary vs Linear Searching Algorithms? - mycodings

Comments are closed.