Pointers
COS1511 - Introduction To Programming I · Learning C++
Pointers
Pointers are a powerful feature in C++. They allow you to directly access and manipulate memory addresses. This can lead to more efficient programs and better control over how data is stored and accessed.
What is a Pointer?
A pointer is a variable that stores the memory address of another variable. Instead of holding a data value, it holds the location in memory where that value is stored. This is useful for dynamic memory allocation and for creating complex data structures.
Remember: A pointer must be declared with a specific type. For example, an integer pointer is declared as int *ptr;.
Declaring and Initialising Pointers
To declare a pointer, you use the asterisk (*) symbol. Here is how you can declare and initialise a pointer:
int my_var = 10; // An integer variable
int *ptr = &my_var; // A pointer to my_varIn this example, my_var is an integer variable that holds the value 10. The pointer ptr is declared to hold the address of my_var using the address-of operator (&).
Accessing the Value of a Pointer
You can access the value stored in the memory address that a pointer points to using the dereference operator (*). Here is an example:
int my_var = 10;
int *ptr = &my_var;
int value = *ptr; // value now holds 10In this example, value will now hold the value of my_var, which is 10. The dereference operator (*) allows you to access the value at the address stored in the pointer.
Watch out: Dereferencing a null pointer or an uninitialised pointer can lead to undefined behaviour. Always ensure that your pointer is pointing to a valid memory address before dereferencing it.
Pointer Arithmetic
C++ allows you to perform arithmetic operations on pointers. This can be useful when working with arrays. When you increment a pointer, it moves to the next memory address of the type it points to. For example:
int arr[3] = {10, 20, 30};
int *ptr = arr; // Points to the first element
ptr++; // Now points to the second elementIn this case, ptr initially points to the first element of the array. After the increment operation, it points to the second element.
Dynamic Memory Allocation
Pointers are essential for dynamic memory allocation in C++. You can allocate memory during runtime using the new keyword. Here is an example:
int *ptr = new int; // Dynamically allocate memory for an integer
*ptr = 25; // Assign value to the allocated memoryIn this example, memory for an integer is allocated on the heap, and ptr points to that memory. You can also allocate memory for an array:
int *arr = new int[5]; // Dynamically allocate memory for an array of 5 integersAfter using dynamically allocated memory, it is important to free it using the delete keyword:
delete ptr; // Free memory allocated for a single integer
delete[] arr; // Free memory allocated for an arrayRemember: Always pair new with delete and new[] with delete[] to avoid memory leaks.
Null Pointers
A null pointer is a pointer that does not point to any valid memory address. It is often used to indicate that the pointer is not yet initialised or that it has been intentionally set to not point to anything. You can create a null pointer like this:
int *ptr = nullptr; // Initialises ptr to a null pointerUsing a null pointer can help prevent accidental dereferencing of uninitialised pointers.
Watch out: Always check if a pointer is null before dereferencing it to avoid runtime errors.
Pointers and Functions
You can use pointers as function parameters to allow functions to modify the original variable. Here is an example:
void modifyValue(int *ptr) {
*ptr = 50; // Change the value at the address pointed to by ptr
}
int main() {
int my_var = 10;
modifyValue(&my_var);
// my_var is now 50
}In this example, the function modifyValue takes a pointer as a parameter. It modifies the value of my_var by dereferencing the pointer.
Pointer to Pointer
You can also have pointers that point to other pointers. This is called a pointer to a pointer. Here is an example:
int my_var = 10;
int *ptr = &my_var;
int **ptr_to_ptr = &ptr; // Pointer to a pointerIn this example, ptr_to_ptr is a pointer that holds the address of ptr. You can dereference it twice to access the original variable:
int value = **ptr_to_ptr; // value now holds 10Common Mistakes with Pointers
When working with pointers, there are several common mistakes to be aware of:
- Dereferencing a null or uninitialised pointer.
- Forgetting to free dynamically allocated memory.
- Using the wrong delete operator for memory allocated with new[].
- Not checking if a pointer is null before dereferencing it.
Watch out: Always be careful with pointer arithmetic to avoid accessing memory out of bounds.
Summary
- A pointer is a variable that stores a memory address.
- Use the & operator to obtain the address of a variable.
- Use the * operator to access the value at the address a pointer points to.
- Dynamic memory allocation is done using new and should be released with delete.
- Null pointers indicate that a pointer does not point to any valid memory.
- Pointers can be passed to functions to modify original variables.
Check your understanding
- What is a pointer and how is it declared in C++?
- Explain the purpose of the dereference operator.
- What is the difference between the new and delete operators?
- How can you create a pointer to a pointer in C++?