Back to: C++
introduction
Some languages (including C and C++) allow you to directly access and manipulate memory, while some do not. This is one of the most powerful features of C++.
This access and manipulation of memory is achieved by using pointers. A pointer is a variable that stores the memory address of an object.
If this access and manipulation of memory is such a powerful feature, then you may wonder why it isn’t present in all languages! One reason is that if used incorrectly, you can run into serious problems!
usage
When you write a program, most of the time you do not need to know the address of a variable. There are specific scenarios where you would want to.
Pointers are used for three main things:
- allocate new objects on the heap
- pass functions to other functions
- iterate over elements in arrays or other data structures
dereference
Sometimes we may want to access the value stored at the memory address pointed to by a pointer. We can do this by using *, which is the dereference operator. This is also called the indirection operator. Using the dereference operator on a pointer retrieves the value stored at the memory location pointed to by that pointer. This way we can access and manipulate data and not just the memory address.
syntax
string name = "Matthew";
string* ptr = &name;
cout << ptr;
When we want to find the address of a variable, we use &, which is the address-of operator.
When we want to dereference a pointer, we use *, the indirection operator.
int p = &i;
int* p = i;
As mentioned, you can run into serious problems with pointers! One such example is trying to dereference a null pointer. This could cause a system crash, specifically a segmentation fault! This is because you are trying to access a pointer that will point to memory that does not exist!
Another problem is when an uninitialised pointer is dereferenced. This can lead to an undefined behaviour.
raw vs smart pointers
The type of pointers that I have talked about so far are called raw pointers. As mentioned, as well as being very powerful, raw pointers can also be dangerous – causing serious problems to your application! In C, raw pointers were used, which meant that these risks were possible. To try and remove some of these dangers, C++ introduced smart pointers – designed to make your programs safer. Raw pointers can still be used but should only be used when vital, such as a large performance benefit.
Pointers exist in other languages but it is often hidden. You can make your own pointers. Pointers can be very powerful. Pointers are a container that stores an address, such as a memory location. & before a variable allows us to get the address of a variable. Example 1 shows the physical address of where this variable is stored. A number like this would be hard for a human to remember, so using a memorable name for a variable is better. We can create a pointer using * and assign it to a variable, this way the variable will store the location of the variable.
dereference pointer:
One of the most powerful features of C++ is its ability to directly manipulate computer memory! This is achieved by using pointers. A pointer is a variable that holds a memory address. When you write a program, most of the time you do not need to know the address of a variable. If you want this information, you can use the address-of operator (&), which will return the address of an object in memory.
include <iostream>
int main() {
}
When used correctly, pointers can speed up code and improve efficiency and flexibility.