Set 1 (Q1-Q20) Set 2 (Q21-Q40)
Q #21) What is the usage of the pointer in C?
Accessing array elements: Pointers are used in traversing through an array of integers and strings. The string is an array of characters which is terminated by a null character ‘\0’.
Dynamic memory allocation: Pointers are used in allocation and deallocation of memory during the execution of a program.
Call by Reference: The pointers are used to pass a reference of a variable to other function.
Data Structures like a tree, graph, linked list, etc.: The pointers are used to construct different data structures like tree, graph, linked list, etc.
Q #22) What is static memory allocation?
In 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.
The lifetime of a variable in static memory is the lifetime of a program.
The static memory is allocated using static keyword.
The static memory is implemented using stacks or heap.
The pointer is required to access the variable present in the static memory.
The static memory is faster than dynamic memory.
In static memory, more memory space is required to store the variable.
For example:
int a[10];
The above example creates an array of integer type, and the size of an array is fixed, i.e., 10.
Q #23) What is dynamic memory allocation?
In case of dynamic memory allocation, memory is allocated at runtime and memory can be increased while executing the program. It is used in the linked list.
The malloc() or calloc() function is required to allocate the memory at the runtime.
An allocation or deallocation of memory is done at the execution time of a program.
No dynamic pointers are required to access the memory.
The dynamic memory is implemented using data segments.
Less memory space is required to store the variable.
For example
int *p= malloc(sizeof(int)*10);
Q #24) What functions are used for dynamic memory allocation in C language?
malloc()
The malloc() function is used to allocate the memory during the execution of the program.
It does not initialize the memory but carries the garbage value.
It returns a null pointer if it could not be able to allocate the requested space.
Syntax
ptr = (cast-type*) malloc(byte-size) // allocating the memory using malloc() function.
calloc()
The calloc() is same as malloc() function, but the difference only is that it initializes the memory with zero value.
Syntax
ptr = (cast-type*)calloc(n, element-size);// allocating the memory using calloc() function.
realloc()
The realloc() function is used to reallocate the memory to the new size.
If sufficient space is not available in the memory, then the new block is allocated to accommodate the existing data.
Syntax
ptr = realloc(ptr, newsize); // updating the memory size using realloc() function.
In the above syntax, ptr is allocated to a new size.
free():The free() function releases the memory allocated by either calloc() or malloc() function.
Syntax
free(ptr); // memory is released using free() function.
The above syntax releases the memory from a pointer variable ptr.