Understanding pointer basics and memory allocation
In this section, we will review pointer basics as well as introduce operators applicable to pointers, such as the address-of operator, the dereference operator, and operators new()
and delete()
. We will employ the address-of operator &
to calculate the address of an existing variable, and conversely, we will apply the dereference operator *
to a pointer variable to go to the address contained within the variable. We will see examples of memory allocation on the heap, as well as how to mark that same memory for potential reuse by returning it to the free list when we are done with it.
Using pointer variables allows our applications to have greater flexibility. At runtime, we can determine the quantity of a certain data type we may need (such as in a dynamically allocated array), organize data in data structures that facilitate sorting (such as in a linked list), or gain speed by passing an address of a large piece of data to...