The Wayback Machine - https://web.archive.org/web/20240917130414/https://www.geeksforgeeks.org/difference-between-static-and-dynamic-memory-allocation-in-c/
Open In App

Difference between Static and Dynamic Memory Allocation in C

Last Updated : 12 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Memory Allocation: Memory allocation is a process by which computer programs and services are assigned with physical or virtual memory space. The memory allocation is done either before or at the time of program execution. There are two types of memory allocations: 
 

  1. Compile-time or Static Memory Allocation
  2. Run-time or Dynamic Memory Allocation

Static Memory Allocation: Static Memory is allocated for declared variables by the compiler. The address can be found using the address of operator and can be assigned to a pointer. The memory is allocated during compile time. 

Dynamic Memory Allocation: Memory allocation done at the time of execution(run time) is known as dynamic memory allocation. Functions calloc() and malloc() support allocating dynamic memory. In the Dynamic allocation of memory space is allocated by using these functions when the value is returned by functions and assigned to pointer variables. 

 

Tabular Difference Between Static and Dynamic Memory Allocation in C:

 

S.No

Static Memory Allocation

Dynamic Memory Allocation

1 In the static memory allocation, variables get allocated permanently, till the program executes or function call finishes. In the Dynamic memory allocation, the memory is controlled by the programmer. It gets allocated whenever a malloc() is executed gets deallocated wherever the free() is executed.
2 Static Memory Allocation is done before program execution. Dynamic Memory Allocation is done during program execution.
3 It uses stack for managing the static allocation of memory It uses heap (not heap data structure) of memory for managing the dynamic allocation of memory
4 It is less efficient It is more efficient
5 In Static Memory Allocation, there is no memory re-usability In Dynamic Memory Allocation, there is memory re-usability and memory can be freed when not required
6 In static memory allocation, once the memory is allocated, the memory size can not change. In dynamic memory allocation, when memory is allocated the memory size can be changed.
7 In this memory allocation scheme, we cannot reuse the unused memory. This allows reusing the memory. The user can allocate more memory when required. Also, the user can release the memory when the user needs it.
8 In this memory allocation scheme, execution is faster than dynamic memory allocation. In this memory allocation scheme, execution is slower than static memory allocation.
9 In this memory is allocated at compile time. In this memory is allocated at run time.
10 In this allocated memory remains from start to end of the program. In this allocated memory can be released at any time during the program.
11 Example: This static memory allocation is generally used for array. Example: This dynamic memory allocation is generally used for linked list.

 


Previous Article
Next Article

Similar Reads

Difference between Static Allocation and Heap Allocation
Static Allocation v/s Heap Allocation Static Allocation: Static allocation is an allocation procedure that is used for the allocation of all the data objects at compile time. In this type of allocation, allocation of data objects is done at compile time only. As in static allocation, the compiler decides the extent of storage which cannot be change
3 min read
Difference between Static allocation and Stack allocation
Static Allocation: Static allocation is a procedure which is used for allocation of all the data objects at compile time. Static allocation is possible only when the compiler knows the size of data object at compile time. In this type of allocation, formation of data objects is not possible under any circumstances at run time. In static allocation,
2 min read
Memory Allocation in Static Data Members in C++
C++ allows defining static data members within a class using the static keyword. When a data member is declared as static, then we must keep the following note in mind: Irrespective of the number of objects created, only a single copy of the static member is created in memory. All objects of a class share the static member. All static data members
4 min read
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
Since C is a structured language, it has some fixed rules for programming. One of them includes changing the size of an array. An array is a collection of items stored at contiguous memory locations. As can be seen, the length (size) of the array above is 9. But what if there is a requirement to change this length (size)? For example, If there is a
9 min read
C | Dynamic Memory Allocation | Question 1
The most appropriate matching for the following pairs (GATE CS 2000) X: m=malloc(5); m= NULL;        1: using dangling pointers Y: free(n); n->value=5;         2: using uninitialized pointers Z: char *p; *p = ’a’;           3. lost memory is: (A) X—1 Y—3 Z-2 (B) (X—2 Y—1 Z-3 (C) X—3 Y—2 Z-1 (D) X—3 Y—1 Z-2 Answer: (D) Explanation: X -> A poin
1 min read
C | Dynamic Memory Allocation | Question 2
Consider the following three C functions : [PI] int * g (void) { int x= 10; return (&x); } [P2] int * g (void) { int * px; *px= 10; return px; } [P3] int *g (void) { int *px; px = (int *) malloc (sizeof(int)); *px= 10; return px; } Which of the above three functions are likely to cause problems with pointers? (GATE 2001) (A) Only P3 (B) Only P1
1 min read
C | Dynamic Memory Allocation | Question 3
Output? C/C++ Code # include # include void fun(int *a) { a = (int*)malloc(sizeof(int)); } int main() { int *p; fun(p); *p = 6; printf(\"%d",*p); return(0); } (A) May not work (B) Works and prints 6 Answer: (A)Explanation: The program is not valid. Try replacing “int *p;” with “int *p = NULL;” and it will try to dereference a null pointer. This is
1 min read
C | Dynamic Memory Allocation | Question 8
Which of the following is/are true (A) calloc() allocates the memory and also initializes the allocates memory to zero, while memory allocated using malloc() has uninitialized data. (B) malloc() and memset() can be used to get the same effect as calloc(). (C) calloc() takes two arguments, but malloc takes only 1 argument. (D) Both malloc() and call
1 min read
C | Dynamic Memory Allocation | Question 5
What is the return type of malloc() or calloc() (A) void * (B) Pointer of allocated memory type (C) void ** (D) int * Answer: (A) Explanation: malloc() and calloc() return void *. We may get warning in C if we don't type cast the return type to appropriate pointer. Quiz of this Question
1 min read
C | Dynamic Memory Allocation | Question 6
Which of the following is true? (A) "ptr = calloc(m, n)" is equivalent to following ptr = malloc(m * n); (B) "ptr = calloc(m, n)" is equivalent to following ptr = malloc(m * n); memset(ptr, 0, m * n); (C) "ptr = calloc(m, n)" is equivalent to following ptr = malloc(m); memset(ptr, 0, m); (D) "ptr = calloc(m, n)" is equivalent to following ptr = mal
1 min read
C | Dynamic Memory Allocation | Question 7
What is the problem with following code? #include<stdio.h> int main() { int *p = (int *)malloc(sizeof(int)); p = NULL; free(p); } (A) Compiler Error: free can't be applied on NULL pointer (B) Memory Leak (C) Dangling Pointer (D) The program may crash as free() is called for NULL pointer. Answer: (B) Explanation: free() can be called for NULL
1 min read
C | Dynamic Memory Allocation | Question 8
Consider the following program, where are i, j and k are stored in memory? int i; int main() { int j; int *k = (int *) malloc (sizeof(int)); } (A) i, j and *k are stored in stack segment (B) i and j are stored in stack segment. *k is stored on heap. (C) i is stored in BSS part of data segment, j is stored in stack segment. *k is stored on heap. (D)
1 min read
What is Dynamic Memory Allocation?
Resources are always a premium. We have strived to achieve better utilization of resources at all times; that is the premise of our progress. Related to this pursuit, is the concept of memory allocation.Memory has to be allocated to the variables that we create, so that actual variables can be brought to existence. Now there is a constraint as how
5 min read
Difference Between Contiguous and Non-Contiguous Memory Allocation
Memory management is one of the fundamentals of operating systems that has implications for its performances and use of resources. These are discussed below: Contiguous and non-contiguous allocation of memory Allocation of memory may take either of the following forms: This article enables you to know the difference between the two, how memory is a
5 min read
Difference between static and non-static variables in Java
There are three types of variables in Java: Local VariablesInstance VariablesStatic Variables The Local variables and Instance variables are together called Non-Static variables. Hence it can also be said that the Java variables can be divided into 2 categories: Static Variables: When a variable is declared as static, then a single copy of the vari
4 min read
Difference Between Static and Non Static Nested Class in Java
Nested classes are divided into two categories namely static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes. A class can either be static or non-static in java. So there is a lot of difference between making a class static or non-static. There are two kind
4 min read
Difference between Static-1 and Static-0 Hazard
A hazard in a digital circuit is a temporary disturbance in the ideal operation of the circuit which if given some time, gets resolved itself. These disturbances occur when different paths from the input to output have different delays. There are different kind of hazards but here we will discuss about the difference between different static hazard
3 min read
Difference between Static and Dynamic SQL
Static or Embedded SQL are SQL statements in an application that do not change at runtime and, therefore, can be hard-coded into the application. Dynamic SQL is SQL statements that are constructed at runtime; for example, the application may allow users to enter their own queries. Dynamic SQL is a programming technique that enables you to build SQL
2 min read
Difference between Static Arrays and Dynamic Arrays
In the Data structure, we know that an array plays a vital role in having similar types of elements arranged in a contiguous manner with the same data type. According to the concept of array, an array can be defined in two ways as per the memory allocation concept. Types of Arrays:There are basically two types of arrays: Static Array: In this type
5 min read
Difference between Static Friction and Dynamic Friction
Friction is defined as the force that opposes the motion between any surfaces, fluid layers, and any other material in contact with each other. Friction is produced due to the irregularities between the surfaces. It produces heat. It depends on the nature of the surfaces. Friction always acts between any two contact surfaces of bodies. Friction is
6 min read
Difference between Static and Dynamic Routing
Routing is a vital communication mechanism that governs how data packets travel from source to destination. Effective routing ensures that data is transferred across networks in an efficient, reliable, and timely manner. There are two main forms of routing: static and dynamic. In this article, we will discuss the differences between static and dyna
3 min read
Difference Between Static and Dynamic Web Pages
There are two basic methods of web design: static and dynamic web pages. Users access static web pages, which present the same content every time they are viewed. On the other hand, dynamic webpages create content instantly in response to user input and present customized or updated information. Let's learn about this in detail in this article. Wha
3 min read
Difference between Static and Dynamic IP address
As you discuss connectivity of devices to the internet, or communication within a network, an IP address is taken as a basic understanding. There are two main types of IP addresses: Static and Dynamic. The purpose of this article is to describe what IP addresses are and the difference between static and dynamic IPs. What is IP Address?IP stands for
6 min read
Difference between Static and Dynamic Hazard
Hazard: In digital circuits, specially in combinational circuits, they give rise to behavior's, glitches and/or wrong output signals. While designing electronics circuits, one must be in a position to distinguish between the static hazards and dynamic hazards. Interference or interruption occurs when due to race condition the signal passes through
4 min read
MCQ on Memory allocation and compilation process
1. What will be the output of the following code? C/C++ Code #include <stdio.h> #include <stdlib.h> int main() { union test { int i; float f; char c; }; union test* t; t = (union test*)malloc(sizeof(union test)); t->f = 20.60f; printf("%f", t->f); return 0; } (a) Garbage value (b) 20.60000
3 min read
If memory allocation using new is failed in C++ then how it should be handled?
In this article, if memory allocation using new is failed in C++ then how it should be handled? When an object of a class is created dynamically using new operator, the object occupies memory in the heap. Below are the major thing that must be keep in mind: What if sufficient memory is not available in the heap memory, and how it should be handled?
3 min read
Partition Allocation Methods in Memory Management
In the operating system, the following are four common memory management techniques. Single contiguous allocation: Simplest allocation method used by MS-DOS. All memory (except some reserved for OS) is available to a process. Partitioned allocation: Memory is divided into different blocks or partitions. Each process is allocated according to the re
4 min read
Stack vs Heap Memory Allocation
Memory in a C/C++/Java program can either be allocated on a stack or a heap.Prerequisite: Memory layout of C program. Stack Allocation: The allocation happens on contiguous blocks of memory. We call it a stack memory allocation because the allocation happens in the function call stack. The size of memory to be allocated is known to the compiler and
7 min read
How to Restrict Dynamic Allocation of Objects in C++?
C++ programming language allows both auto(or stack-allocated) and dynamically allocated objects. In Java & C#, all objects must be dynamically allocated using new. C++ supports stack-allocated objects for the reason of runtime efficiency. Stack-based objects are implicitly managed by the C++ compiler. They are destroyed when they go out of scop
2 min read
Difference between Random Access Memory (RAM) and Content Addressable Memory (CAM)
Random Access Memory (RAM) is used to read and write. It is the part of primary memory and used in order to store running applications (programs) and program's data for performing operation. It is mainly of two types: Dynamic RAM (or DRAM) and Static RAM (or SRAM). RAM is made up of small memory cells that are arranged in a grid pattern. Each cell
4 min read