Pointers in C++
Pointers:
Pointer is a variable which store the address of another variable. It is declared using '*' with data type of the variable whose address will be store in the pointer variable.
For Example -
int n=10; // An integer variable n
int* ptr = &n; // ptr is a pointer to the variable n.
// ptr store the address of variable n.
int*ptr means "ptr is a pointer to the integer variable".
Dereferencing of a pointer :
We dereference a pointer variable to get the value stored at the address stored in the pointer variable.
e.g.
int temp = 100;
int *ptr;
ptr = &temp;
printf("%d", *ptr); // It will print the 100
In C++, there are three types of pointers:
Null pointers: A null pointer is a pointer that does not point to any memory location. It is represented by the value "0" or "nullptr". Null pointers are often used to indicate that a pointer does not currently point to a valid object or data structure.
Void pointers: A void pointer is a pointer that can point to any data type, including primitive data types (such as int or float) and user-defined data types (such as classes or structs). However, because a void pointer has no associated data type, you cannot dereference it directly. Instead, you must first cast it to a pointer of the desired type.
Typed pointers: A typed pointer is a pointer that is explicitly declared to point to a specific data type. For example, you can declare a pointer to an int, float, or any other data type. Typed pointers are the most commonly used type of pointers in C++, as they allow you to work with specific data types directly and safely.
1. Wild Pointers :
A wild pointer in C++ is a pointer that has not been initialized or has been initialized to an invalid memory address. When a pointer is declared but not initialized or is initialized to a random value, it may point to an unpredictable memory location that may or may not be valid.
Using a wild pointer can result in undefined behavior, as the pointer may be pointing to an invalid or inaccessible memory location, causing the program to crash or produce unexpected results. In some cases, using a wild pointer can even corrupt the program's memory, leading to more severe issues.
It's important to always initialize pointers before using them to ensure they point to a valid memory location. Additionally, it's good practice to set pointers to nullptr when they are no longer needed to prevent them from becoming wild pointers.
2. Dangling Pointer :
Dangling pointers are pointers that point to an object or memory location that has been deallocated, released, or otherwise invalidated.
Dangling pointers can occur in C++ when a pointer is deleted or goes out of scope, but other pointers or variables still reference the same memory location. Using a dangling pointer can lead to undefined behavior, as the memory location may no longer contain the expected data, or it may have been overwritten by other data.
To prevent dangling pointers, it's important to ensure that pointers are always pointing to a valid memory location.
This can be achieved by:
Initialising pointers to nullptr or a valid memory location.
Avoiding deleting or deallocating memory that is still being used or referenced by other pointers.
Resetting or nullifying pointers after deallocating or releasing the memory they point to.
If you encounter a dangling pointer in your code, it's important to identify and fix the root cause of the problem to prevent potential bugs and security vulnerabilities.
3. Smart Pointers :
Smart pointers are a type of pointer in C++ that provide automatic memory management and help prevent common memory management errors, such as memory leaks and dangling pointers.
There are two types of smart pointers in C++: unique_ptr and shared_ptr.
Unique_ptr: Unique_ptr is a type of smart pointer that manages a unique (non-shared) object. It ensures that the object it points to is destroyed when the unique_ptr is destroyed or when it is reset. Unique_ptr is move-only, meaning it cannot be copied.
Shared_ptr: Shared_ptr is a type of smart pointer that manages a shared object, meaning it can be accessed by multiple shared_ptr instances. It keeps track of the number of shared_ptr instances that point to the object and destroys the object when the last shared_ptr instance is destroyed or reset.
Both unique_ptr and shared_ptr use RAII (Resource Acquisition Is Initialization) technique to ensure automatic memory management. They are implemented as templates and provide type-safe and efficient memory management.
Using smart pointers can make C++ code more robust, efficient, and easier to maintain. However, it's important to note that smart pointers are not a silver bullet for all memory management problems and should be used in conjunction with other memory management techniques, such as proper object lifetime management and avoiding circular references.
4. Function Pointers :
Function pointers in C++ are pointers that point to functions. They can be used to call functions indirectly or to pass functions as arguments to other functions.
The syntax for declaring a function pointer is as follows:
For example, to declare a function pointer that points to a function that takes two integers as parameters and returns an integer, you would use the following syntax:
int (*my_function_pointer)(int, int);
To assign a function to a function pointer, you can use the address-of operator (&) followed by the function name:
my_function_pointer = &my_function;
You can then call the function indirectly using the function pointer:
int result = (*my_function_pointer)(1, 2);
Alternatively, you can use the shorthand syntax to call the function using the function pointer:
int result = my_function_pointer(1, 2);
Function pointers are commonly used in callback functions, where a function is passed as an argument to another function to be called later. They are also used in object-oriented programming to implement virtual functions and function pointers to member functions.
Comments
Post a Comment