What is static memory allocation and dynamic memory allocation?

28 Dec 2022 Balmiki Mandal 0 C Programming

Static vs. Dynamic Memory Allocation: Understanding the Differences

In computer programming, static memory allocation and dynamic memory allocation are two ways of allocating memory to a program.

Static memory allocation:

The compiler allocates the required memory space for a declared variable.By using the address of operator,the reserved address is obtained and this address may be assigned to a pointer variable.Since most of the declared variable have static memory,this way of assigning pointer value to a pointer variable is known as static memory allocation. memory is assigned during compilation time.

Static memory allocation is a process in which the memory required for a program is allocated at compile-time. In other words, the memory is assigned to the program before it runs, and it remains fixed throughout the program's execution. Static memory allocation is typically used for variables that have a fixed size and are known at compile-time, such as global variables or constants.

 

Dynamic memory allocation:

It uses functions such as malloc( ) or calloc( ) to get memory dynamically.If these functions are used to get memory dynamically and the values returned by these functions are assingned to pointer variables, such assignments are known as dynamic memory allocation.memory is assined during run time.

Dynamic memory allocation, on the other hand, is a process in which memory is allocated to a program at run-time. This means that the memory can be allocated or de-allocated as needed while the program is running. Dynamic memory allocation is typically used for data structures that have a variable size and are created during the program's execution, such as arrays or linked lists.

 

Some Important Point of static or dynamic memory allocation 

One advantage of static memory allocation is that it can be faster and more efficient than dynamic memory allocation, since the memory is allocated in advance and does not need to be managed during the program's execution. However, static memory allocation can also lead to wasted memory if the allocated memory is not fully utilized.

Dynamic memory allocation, on the other hand, allows programs to use memory more efficiently, since memory can be allocated and de-allocated as needed. However, dynamic memory allocation requires more overhead and can be slower than static memory allocation, since the memory must be managed during the program's execution.

 

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.