Dynamic Memory allocation in C
There are 2 types of Memory allocation: 1. Static memory allocation, 2. Dynamic memory allocation.
The concept of Dynamic memory allocation in C language enables the C programmer to allocate memory at runtime.
Possible by 4 function of stdlib.h header file.
1. malloc() : allocates single block of requested memory
2. calloc() : allocates multiple block of requested memory
3. realloc() : reallocates the memory occupied by malloc() or calloc() functions
4. free() : frees the dynamically allocated memory.
Malloc()
- Malloc stands for memory allocation.
- Mainly used to allocate memory to a pointer variable or for a structure also during execution time.
- Allocates required size in terms of bytes and return pointer to the starting address of memory block.
- Used to dynamically allocate a large single block of memory with specified size.
- Defined in <stdlib.h> C header file.
- Malloc() uses sizeof() function to define its size. sizeof() is a predefined C function that returns the size of variable.
- example: int a;
- printf(sizeof(a));
- Syntax : Datatype ptr = (cast_type*) malloc (size_type);
which means integer memory space is 4. which gives 50*4=200 bytes.
- Malloc() will return NULL in 2 cases:
- 1. memory space is insufficient
- 2. malloc() fails to assign memory
- Example:
malloc program to print n number of elements
Calloc()
- The calloc() stands for contiguous memory allocation.
- This function allocates multiple block of requested memory.
- It initially initialize all bytes to zero.
- It returns NULL if memory is not sufficient.
- It allocates multiple block of memory.
- Defined in <stdlib.h>
- Syntax : Datatype ptr = (cast_type*) calloc (n,size_type);
- Example:
calloc program to print n number of elements
Realloc()
- The realloc function refers to reallocation of dynamic memory, whenever we allocate the memory with the help of malloc or calloc.
- If the allocated memory is insufficient or more, we need to use realloc to reallocate the memory.
- Once we reallocate the memory, it resize the existing as per the requirement.
- Syntax : Datatype ptr = realloc(ptr,newsize);
- Example:
Realloc example
Free()
- The memory occupied by malloc() or calloc() functions must be released by calling free() function. Otherwise, it will consume memory until program exit.
- Syntax : free(ptr);
- Example:
Pointers
The pointer in C language is a variable which stores the address of another
variable. This variable can be of type int, char, array, function, or any other
pointer. The size of the pointer depends on the architecture. However, in 32
bit architecture the size of a pointer is 2 byte.
Example:
1. int n = 10;
2. int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer.
Declaring a pointer
The pointer in c language can be declared using * (asterisk symbol). It is also
known as indirection pointer used to dereference a pointer.
1. int *a;//pointer to int
2 . char *c;//pointer to char
NULL Pointer
A pointer that is not assigned any value but NULL is known as the NULL
pointer. If you don't have any address to be specified in the pointer at the
time of declaration, you can assign NULL value. It will provide a better
approach.
Dangling Pointers
A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer. Such a situation can lead to unexpected behavior in the program and also serve as a source of bugs in C programs.
There are three different ways where a pointer acts as a dangling pointer:
1. De-allocation of Memory
When a memory pointed by a pointer is deallocated the pointer becomes a dangling pointer.
2. Function Call
When the local variable is not static and the function returns a pointer to that local variable. The pointer pointing to the local variable becomes dangling pointer.
3. Variable Goes Out of Scope
When a variable goes out of scope the pointer pointing to that variable becomes a dangling pointer.
Questions:
1.Explain the differences between malloc and calloc, functions in C programming.
malloc:2.Discuss the concept of an array of pointers in C programming.
To declare an array of pointers in C, you specify the base type followed by an asterisk (*) to indicate that it's a pointer, and then use square brackets [] to denote the array size.
Initializing an Array of Pointers:
There are different ways to initialize an array of pointers. You can initialize each pointer individually or assign the pointers to the addresses of existing variables or dynamically allocate memory using functions like malloc()
Accessing Elements of an Array of Pointers:
Once an array of pointers is initialized, you can access the elements by using array indexing and dereferencing the pointers.
Example:
#include <stdio.h> int main() { // Array of pointers to strings char *names[3] = {"Hello", "World", "C"}; // Accessing and printing strings using array of pointers for (int i = 0; i < 3; i++) { printf("Name %d: %s\n", i + 1, names[i]); } return 0; }
3.Define what a dangling pointer is in the context of C
programming. or
What are the situations where a pointer becomes dangling pointer?
There are three different ways where a pointer acts as a dangling pointer:
When a memory pointed by a pointer is deallocated the pointer becomes a dangling pointer.
2. Function Call
When the local variable is not static and the function returns a pointer to that local variable. The pointer pointing to the local variable becomes dangling pointer.
3. Variable Goes Out of Scope
When a variable goes out of scope the pointer pointing to that variable becomes a dangling pointer.
4. Explain realloc and free function with example.
realloc
- The realloc function refers to reallocation of dynamic memory, whenever we allocate the memory with the help of malloc or calloc.
- If the allocated memory is insufficient or more, we need to use realloc to reallocate the memory.
- Once we reallocate the memory, it resize the existing as per the requirement.
- Syntax : Datatype ptr = realloc(ptr,newsize);
- Example:
Free()
- The memory occupied by malloc() or calloc() functions must be released by calling free() function. Otherwise, it will consume memory until program exit.
- Syntax : free(ptr);
- Example: