Dynamic memory allocation

Describe dynamic memory.

  • The process of allocating memory at runtime is known as dynamic memory allocation. The idea of dynamic memory allocation is used to minimise memory waste and is the best method of memory allocation. We’ll look at the C programming language’s memory structure to fully understand the idea.

Memory Allocation in C:

  • There are four different parts to memory allocation in C. Because understanding each of them is essential for understanding memory layout, we shall go into detail about each of them.

Code:

  • All of our program’s text segments are composed of code. Since we are already familiar with this section, we won’t go into more depth about it.

Variable

  • We refer to both global and static variables as variables. While static variables have restrictions inside the function, global variables can be utilised elsewhere in the programme. Depending on the amount of data they can store, the variable section is further separated into two segments.

Data segment:

  • Stores data that has already been initialised, or whose value has been determined.

Example:

int i = 0;

The bss segment stores uninitialized data, or data whose variables have not yet been initialised.

Example:

int i = 0;

Stack:

  • A LIFO data structure is the stack. It grows in size as the programme progresses.
  • The stack initially resembles a bucket, where the last item entered will be the first one to emerge. Because of this, it is known as a LIFO data structure, or last in, first out, structure.Consider adding function A to the stack. The execution of Function A will begin. As it continues to run, function A is now calling function B. The programme will begin executing Function B after pushing B into the stack. If B now calls a different function C, the programme will add C to the stack and begin C’s execution.Now that C has finished running through its whole, the programme will remove C from the stack (because it was the last one to enter) and begin running B. After B has finished running entirely, it will be pushed out, and A will then begin running until the stack is empty.

Stack overflow:

  • Stack Overflow is a phenomena that occurs when a stack runs out of space due to poor programming or a logical mistake.

Heap:

  • Heap is a data structure that is built on trees. When we allocate memory dynamically, its size grows. We must establish a pointer in our main function that points to a memory block in a heap in order to use the heap data structure. The drawback of using heap is that when the pointer is rewritten, the memory does not immediately free itself.

#include <stdio.h>
#include <string.h>
int i;
int main()
{
    int id;
    return 0;
}
 
Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in