Static memory vs Dynamic memory

Introduction

Computer memory is a critical component of any computer system, and it can be broadly categorized into two types: static memory and dynamic memory.

Static memory and dynamic memory differ in terms of their allocation and size, and each type of memory is used for different purposes. In this article, we will explore the differences between static memory and dynamic memory and how they are used in computer systems. this won't just be a typical article explaining Static and Dynamic memory, we'll discuss the myths about them and myth bust in a simple way.

What is Static memory?

Static memory is a type of memory that is allocated at the execution (run time)of a program. This memory is typically used for storing variables or data that do not change during the execution of a program. Static memory is allocated in the stack or data segments of the program, and its size is fixed at run time. This means that the amount of static memory used by a program is determined when the program is compiled.

Here's an example in C language.

int array[10] = {21,23,32,36,67,38};
char av[25] = "Coding is fun";

Static memory is the memory that is fixed in compilation even though the allocation is done at the run time there's a common myth that Static memory allocation happens during compilation which is not true. both Static and Dynamic memory is allocated at run time only but unlike Static memory Dynamic m]emory can be modified at run time that's the significant difference between static and dynamic memory.

What is Dynamic memory?

Dynamic memory allocation is a way in which the size of a data structure can be changed during the run time of a program, in other words, memory can be allocated, de-allocated, and modified at the time of execution.

Dynamic memory is allocated in the heap segment of the program, and its size can be adjusted as per requirements during runtime.

Examples of dynamic memory include dynamic arrays, linked lists, and other data structures that can grow or shrink as needed. These data structures are often used when the size of the data may change during runtime.

here's an example in C language:

int *ptr;
ptr = (int *)malloc(10*sizeof(int));
ptr = (int *)realloc(ptr,20*sizeof(int));

Static memory vs Dynamic memory

There's a popular myth that states Static memory is allocated at compile-time, which isn't true. a compiler's task is to translate the instructions of the program into machine code (binary) and to check the syntax of the program. It won't allocate the memory, it just recognizes the memory required by the program.

both Static and dynamic memory is allocated at the run time and the main difference between them is Static memory is fixed and can't be modified during the run time whereas Dynamic memory can be modified during the run time.

some more myths

There are some popular myths about static and dynamic memory allocation across various programming languages. there's a myth that says C and C++ languages are static memory languages and java is a dynamic memory language which is not factual. whether it is static memory or dynamic memory it is completely in the hands of programmers.

a programmer will decide whether memory is static or dynamic if you fix the size that's called static memory if you write the logic to increase and decrease the memory that is called dynamic memory.

hence in any language memory is allocated at run time only.