Interview :: C
Following are the differences between a call by value and call by reference are:
Call by value | Call by reference | |
---|---|---|
Description | When a copy of the value is passed to the function, then the original value is not modified. | When a copy of the value is passed to the function, then the original value is modified. |
Memory location | Actual arguments and formal arguments are created in separate memory locations. | Actual arguments and formal arguments are created in the same memory location. |
Safety | In this case, actual arguments remain safe as they cannot be modified. | In this case, actual arguments are not reliable, as they are modified. |
Arguments | The copies of the actual arguments are passed to the formal arguments. | The addresses of actual arguments are passed to their respective formal arguments. |
Example of call by value:
Output:
Value of a is: 10 Value of b is: 20
Example of call by reference:
Output:
Value of a is: 13 Value of b is: 17
When a function calls itself, and this process is known as recursion. The function that calls itself is known as a recursive function.
Recursive function comes in two phases:
- Winding phase
- Unwinding phase
Winding phase: When the recursive function calls itself, and this phase ends when the condition is reached.
Unwinding phase: Unwinding phase starts when the condition is reached, and the control returns to the original call.
Example of recursion
Output:
factorial of a number is 120
An Array is a group of similar types of elements. It has a contiguous memory location. It makes the code optimized, easy to traverse and easy to sort. The size and type of arrays cannot be changed after its declaration.
Arrays are of two types:
- One-dimensional array: One-dimensional array is an array that stores the elements one after the another.
Syntax:
- Multidimensional array: Multidimensional array is an array that contains more than one array.
Syntax:
Example of an array:
Output:
1 2 3 4 5
A pointer is a variable that refers to the address of a value. It makes the code optimized and makes the performance fast. Whenever a variable is declared inside a program, then the system allocates some memory to a variable. The memory contains some address number. The variables that hold this address number is known as the pointer variable.
For example:
The above syntax tells that p is a pointer variable that holds the address number of a given data type value.
Example of pointer
Output:
Address value of 'a' variable is 428781252
- 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.
A pointer which can access all the 16 segments (whole residence memory) of RAM is known as far pointer. A far pointer is a 32-bit pointer that obtains information outside the memory in a given section.
- If a pointer is pointing any memory location, but meanwhile another pointer deletes the memory occupied by the first pointer while the first pointer still points to that memory location, the first pointer will be known as a dangling pointer. This problem is known as a dangling pointer problem.
- Dangling pointer arises when an object is deleted without modifying the value of the pointer. The pointer points to the deallocated memory.
Let's see this through an example.
In the above example, initially memory is allocated to the pointer variable ptr, and then the memory is deallocated from the pointer variable. Now, pointer variable, i.e., ptr becomes a dangling pointer.
How to overcome the problem of a dangling pointer
The problem of a dangling pointer can be overcome by assigning a NULL value to the dangling pointer. Let's understand this through an example:
In the above example, after deallocating the memory from a pointer variable, ptr is assigned to a NULL value. This means that ptr does not point to any memory location. Therefore, it is no longer a dangling pointer.
In case of a pointer to pointer concept, one pointer refers to the address of another pointer. The pointer to pointer is a chain of pointers. Generally, the pointer contains the address of a variable. The pointer to pointer contains the address of a first pointer. Let's understand this concept through an example:
In the above example, pptr is a double pointer pointing to the address of the ptr variable and ptr points to the address of 'a' variable.
- 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.
The above example creates an array of integer type, and the size of an array is fixed, i.e., 10.