Memory allocation in C refers to the process of reserving memory space during the execution of a program for storing variables or data structures. There are mainly two types of memory allocation in C: stack allocation and heap allocation.
Stack Allocation:
- Stack allocation is the default method for allocating memory for local variables within functions.
- Memory allocated on the stack is automatically managed by the compiler.
- Variables allocated on the stack have a fixed size and are deallocated when they go out of scope, typically when the function exits.
- Stack memory is limited and generally smaller than heap memory.
Heap Allocation:
- Heap allocation involves dynamically allocating memory during runtime from the heap, which is a larger pool of memory managed by the operating system.
- Memory allocated on the heap must be explicitly requested and released by the programmer using functions like
malloc()
,calloc()
,realloc()
, andfree()
. - Heap memory allows for dynamic data structures like linked lists, trees, and dynamic arrays.
- Improper use of heap memory can lead to memory leaks or memory fragmentation.
Contents
Common memory allocation functions in C:
malloc(size_t size)
: Allocates a block of memory of specified size in bytes and returns a pointer to the allocated memory.calloc(size_t num, size_t size)
: Allocates an array of elements initialized to zero, with the total size equal tonum * size
bytes, and returns a pointer to the allocated memory.realloc(void* ptr, size_t size)
: Resizes the previously allocated memory block pointed to byptr
to the new size specified bysize
. It may move the memory block to a new location, and the contents of the original block may be copied to the new block.free(void* ptr)
: Releases the memory block previously allocated bymalloc
,calloc
, orrealloc
.
Proper memory management is crucial in C programming to prevent memory leaks, segmentation faults, and other memory-related issues. It’s essential to allocate memory dynamically only when necessary and deallocate it when no longer needed to ensure efficient memory usage.
Static Memory allocation
In the case of static memory allocation, memory is allocated at compile time and memory can’t be increased while executing the program. It is used in the array
Dynamic memory allocation
In the case of dynamic memory allocation, memory is allocated at run time and memory can be increased while executing the program. It is used in the linked list.
The following functions are used in Dynamic memory allocation
- malloc() – allocates a single block of requested memory. It has garbage value initially.
- calloc() – allocates multiple blocks of requested memory. It initially initializes all bytes to zero.
- realloc() – reallocates the memory occupied by malloc() or calloc() functions.
- free() – free the dynamically allocated memory.
Stack grows upwards or downwards
Write a C code to check whether a stack grows upwards or downwards
void main()
{
int i=2;
int j=3;
if(&i > &j)
{
printf("stack grown downwards");
}
else
{
printf("stack grows upwards");
}
}
Memory Layout & Storage Type understanding
char *getString()
{
char *str = "Nice test for strings";
return str;
}
int main()
{
printf("%s", getString());
getchar();
return 0;
}
Output: “Nice test for strings”
Explanations:
- The above program works because string constants are stored in Data Section (not in Stack Section). So, when getString returns *str is not lost.
- string constants are stored in Data Section (not in Stack Section)
- static variable and is stored in Data Section,
- array variables are stored in Stack Section