Pointers
Introduction#
A pointer is an address that refers to a location in memory. They’re commonly used to allow functions or data structures to know of and modify memory without having to copy the memory referred to. Pointers are usable with both primitive (built-in) or user-defined types.
Pointers make use of the “dereference” *
, “address of” &
, and “arrow” ->
operators. The ’*’ and ’->’ operators are used to access the memory being pointed at, and the &
operator is used to get an address in memory.
Syntax#
- <Data type> *<Variable name>;
- <Data type> *<Variable name> = &<Variable name of same Data type>;
- <Data type> *<Variable name> = <Value of same Data type>;
- int *foo; //A pointer which would points to an integer value
- int *bar = &myIntVar;
- long *bar[2];
- long *bar[] = {&myLongVar1, &myLongVar2}; //Equals to: long *bar[2]
Remarks#
Be aware of problems when declaring multiple pointers on the same line.
int* a, b, c; //Only a is a pointer, the others are regular ints.
int* a, *b, *c; //These are three pointers!
int *foo[2]; //Both *foo[0] and *foo[1] are pointers.
Pointer basics
Note: in all the following, the existence of the C++11 constant nullptr
is assumed. For earlier versions, replace nullptr
with NULL
, the constant that used to play a similar role.
Creating a pointer variable
A pointer variable can be created using the specific *
syntax, e.g. int *pointer_to_int;
.
When a variable is of a pointer type (int *
), it just contains a memory address. The memory address is the location at which data of the underlying type (int
) is stored.
The difference is clear when comparing the size of a variable with the size of a pointer to the same type:
// Declare a struct type `big_struct` that contains
// three long long ints.
typedef struct {
long long int foo1;
long long int foo2;
long long int foo3;
} big_struct;
// Create a variable `bar` of type `big_struct`
big_struct bar;
// Create a variable `p_bar` of type `pointer to big_struct`.
// Initialize it to `nullptr` (a null pointer).
big_struct *p_bar0 = nullptr;
// Print the size of `bar`
std::cout << "sizeof(bar) = " << sizeof(bar) << std::endl;
// Print the size of `p_bar`.
std::cout << "sizeof(p_bar0) = " << sizeof(p_bar0) << std::endl;
/* Produces:
sizeof(bar) = 24
sizeof(p_bar0) = 8
*/
Taking the address of another variable
Pointers can be assigned between each other just as normal variables; in this case, it is the memory address that is copied from one pointer to another, not the actual data that a pointer points to.
Moreover, they can take the value nullptr
which represents a null memory location. A pointer equal to nullptr
contains an invalid memory location and hence it does not refer to valid data.
You can get the memory address of a variable of a given type by prefixing the variable with the address of operator &
. The value returned by &
is a pointer to the underlying type which contains the memory address of the variable (which is valid data as long as the variable does not go out of scope).
// Copy `p_bar0` into `p_bar_1`.
big_struct *p_bar1 = p_bar0;
// Take the address of `bar` into `p_bar_2`
big_struct *p_bar2 = &bar;
// p_bar1 is now nullptr, p_bar2 is &bar.
p_bar0 = p_bar2;
// p_bar0 is now &bar.
p_bar2 = nullptr;
// p_bar0 == &bar
// p_bar1 == nullptr
// p_bar2 == nullptr
In contrast with references:
- assigning two pointers does not overwrite the memory that the assigned pointer refers to;
- pointers can be null.
- the address of operator is required explicitly.
Accessing the content of a pointer
As taking an address requires &
, as well accessing the content requires the usage of the dereference operator *
, as a prefix. When a pointer is dereferenced, it becomes a variable of the underlying type (actually, a reference to it). It can then be read and modified, if not const
.
(*p_bar0).foo1 = 5;
// `p_bar0` points to `bar`. This prints 5.
std::cout << "bar.foo1 = " << bar.foo1 << std::endl;
// Assign the value pointed to by `p_bar0` to `baz`.
big_struct baz;
baz = *p_bar0;
// Now `baz` contains a copy of the data pointed to by `p_bar0`.
// Indeed, it contains a copy of `bar`.
// Prints 5 as well
std::cout << "baz.foo1 = " << baz.foo1 << std::endl;
The combination of *
and the operator .
is abbreviated by ->
:
std::cout << "bar.foo1 = " << (*p_bar0).foo1 << std::endl; // Prints 5
std::cout << "bar.foo1 = " << p_bar0->foo1 << std::endl; // Prints 5
Dereferencing invalid pointers
When dereferencing a pointer, you should make sure it points to valid data. Dereferencing an invalid pointer (or a null pointer) can lead to memory access violation, or to read or write garbage data.
big_struct *never_do_this() {
// This is a local variable. Outside `never_do_this` it doesn't exist.
big_struct retval;
retval.foo1 = 11;
// This returns the address of `retval`.
return &retval;
// `retval` is destroyed and any code using the value returned
// by `never_do_this` has a pointer to a memory location that
// contains garbage data (or is inaccessible).
}
In such scenario, g++
and clang++
correctly issue the warnings:
(Clang) warning: address of stack memory associated with local variable 'retval' returned [-Wreturn-stack-address]
(Gcc) warning: address of local variable ‘retval’ returned [-Wreturn-local-addr]
Hence, care must be taken when pointers are arguments of functions, as they could be null:
void naive_code(big_struct *ptr_big_struct) {
// ... some code which doesn't check if `ptr_big_struct` is valid.
ptr_big_struct->foo1 = 12;
}
// Segmentation fault.
naive_code(nullptr);
Pointer Operations
There are two operators for pointers: Address-of operator (&): Returns the memory address of its operand. Contents-of (Dereference) operator(*): Returns the value of the variable located at the address specified by its operator.
int var = 20;
int *ptr;
ptr = &var;
cout << var << endl;
//Outputs 20 (The value of var)
cout << ptr << endl;
//Outputs 0x234f119 (var's memory location)
cout << *ptr << endl;
//Outputs 20(The value of the variable stored in the pointer ptr
The asterisk (*) is used in declaring a pointer for simple purpose of indicating that it is a pointer. Don’t confuse this with the dereference operator, which is used to obtain the value located at the specified address. They are simply two different things represented with the same sign.
Pointer Arithmetic
Increment / Decrement
A pointer can be incremented or decremented (prefix and postfix). Incrementing a pointer advances the pointer value to the element in the array one element past the currently pointed to element. Decrementing a pointer moves it to the previous element in the array.
Pointer arithmetic is not permitted if the type that the pointer points to is not complete. void
is always an incomplete type.
char* str = new char[10]; // str = 0x010
++str; // str = 0x011 in this case sizeof(char) = 1 byte
int* arr = new int[10]; // arr = 0x00100
++arr; // arr = 0x00104 if sizeof(int) = 4 bytes
void* ptr = (void*)new char[10];
++ptr; // void is incomplete.
If a pointer to the end element is incremented, then the pointer points to one element past the end of the array. Such a pointer cannot be dereferenced, but it can be decremented.
Incrementing a pointer to the one-past-the-end element in the array, or decrementing a pointer to the first element in an array yields undefined behavior.
A pointer to a non-array object can be treated, for the purposes of pointer arithmetic, as though it were an array of size 1.
Addition / Subtraction
Integer values can be added to pointers; they act as incrementing, but by a specific number rather than by 1. Integer values can be subtracted from pointers as well, acting as pointer decrementing. As with incrementing/decrementing, the pointer must point to a complete type.
char* str = new char[10]; // str = 0x010
str += 2; // str = 0x010 + 2 * sizeof(char) = 0x012
int* arr = new int[10]; // arr = 0x100
arr += 2; // arr = 0x100 + 2 * sizeof(int) = 0x108, assuming sizeof(int) == 4.
Pointer Differencing
The difference between two pointers to the same type can be computed. The two pointers must be within the same array object; otherwise undefined behavior results.
Given two pointers P
and Q
in the same array, if P
is the i
th element in the array, and Q
is the j
th element, then P - Q
shall be i - j
. The type of the result is std::ptrdiff_t
, from <cstddef>
.
char* start = new char[10]; // str = 0x010
char* test = &start[5];
std::ptrdiff_t diff = test - start; //Equal to 5.
std::ptrdiff_t diff = start - test; //Equal to -5; ptrdiff_t is signed.