Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
What do you mean by static memory allocation in C programming?
Static memory allocation in C refers to memory allocation that happens at compile time. The memory size and location are determined before the program runs, and once allocated, the memory cannot be resized during program execution.
Syntax
datatype variable_name[size]; datatype variable_name = value;
Characteristics of Static Memory Allocation
- Compile-time allocation: Memory is allocated during compilation
- Fixed size: Once declared, the size cannot be changed
- Stack storage: Uses stack memory for local variables
- Automatic deallocation: Memory is freed when variable goes out of scope
-
Address accessibility: Address can be obtained using the
&operator
Example 1: Basic Array Declaration
Static memory allocation is commonly used for arrays with fixed size −
#include <stdio.h>
int main() {
int a[5] = {10, 20, 30, 40, 50};
int i;
printf("Elements of the array are: ");
for (i = 0; i < 5; i++) {
printf("%d ", a[i]);
}
printf("<br>");
return 0;
}
Elements of the array are: 10 20 30 40 50
Example 2: Sum and Product Calculation
Let's calculate sum and product of all elements in a statically allocated array −
#include <stdio.h>
int main() {
/* Declaring the array - compile time allocation */
int array[5] = {10, 20, 30, 40, 50};
int i, sum = 0, product = 1;
/* Calculating sum and product */
for (i = 0; i < 5; i++) {
sum = sum + array[i];
product = product * array[i];
}
/* Displaying results */
printf("Sum of elements in the array is: %d<br>", sum);
printf("Product of elements in the array is: %d<br>", product);
return 0;
}
Sum of elements in the array is: 150 Product of elements in the array is: 12000000
Example 3: Memory Address Demonstration
This example shows how to access memory addresses of statically allocated variables −
#include <stdio.h>
int main() {
int num = 42;
char ch = 'A';
float pi = 3.14f;
printf("Variable values and their memory addresses:<br>");
printf("num = %d, Address = %p<br>", num, (void*)&num);
printf("ch = %c, Address = %p<br>", ch, (void*)&ch);
printf("pi = %.2f, Address = %p<br>", pi, (void*)&pi);
return 0;
}
Variable values and their memory addresses: num = 42, Address = 0x7fff5fbff6ac ch = A, Address = 0x7fff5fbff6ab pi = 3.14, Address = 0x7fff5fbff6a4
Advantages and Disadvantages
| Advantages | Disadvantages |
|---|---|
| Fast access speed | Fixed memory size |
| No memory fragmentation | Memory waste if not fully used |
| Automatic memory management | Cannot handle dynamic requirements |
| No risk of memory leaks | Stack overflow risk for large data |
Conclusion
Static memory allocation provides a simple and efficient way to manage memory for fixed-size data. It's ideal for scenarios where memory requirements are known at compile time, offering fast access and automatic cleanup.
Advertisements
