Unit 06: CS 200 review - Pointers
This week's stuff:
You can read through / play the audio for each slide at your own pace, or
Here's an array of floats, and the address of each element:
ELEMENT: arr[0] arr[1] arr[2] arr[3] arr[4] ADDRESS (HEX): 0x7ffd67be86e0 0x7ffd67be86e4 0x7ffd67be86e8 0x7ffd67be86ec 0x7ffd67be86f0 ADDRESS (DEC): 140726343993056 140726343993060 140726343993064 140726343993068 140726343993072 ^ Each element is 4 bytes apart in memory
A float
has a size of 4 bytes, and each element in the array sequence is different by only 4 bytes
as well - each element of an array is contiguous in memory.
int number; |
int* ptr; |
new
keyword with a pointer, we allocate memory dynamically in heap space.
Declaring a pointer requires specifying a data type,
the asterisk symbol * , and a variable name for the pointer.
Note that the asterisk can be attached to the data type, variable name, or
on its own. I prefer attaching it to the data type.
|
Assigning an address to a pointer -
The ampersand & put before a variable will give you
its address - this is known as the address-of operator.
When not in use, a pointer should be initialized to nullptr
for safety.
|
Copying an address from one pointer to another - A basic assignment statement will copy the value from one variable to another. This also works with pointers, since pointer values are addresses. | Dereferencing a pointer requires prefixing the pointer's name with an asterisk. Dereferencing a pointer allows us to access the value stored within the address that the pointer is pointing to. |
|
|
|
|
We can allocate and deallocate memory on the heap using pointers. We could do this for one variable or an array.
Variables | Arrays |
---|---|
Allocate memory for a single variable:
Deallocate memory for a single variable:
|
Allocate memory for an array:
Deallocate memory for an array:
|
We can "resize" a dynamic array... though we're not really resizing it. Because arrays must be contiguous in memory, instead we actually just allocate more memory, copy data over, and free the old memory address, updating the pointer to the new address. Here are the steps:
1. Create a new, bigger array: |
|
2. Copy information from old to new: |
|
3. Free old memory space: |
|
4. Update address: |
|
5. Update size: |
|
Why bother allocating space for a single variable? This concept is foundational to building linked data structures such as a Linked List or Binary Search Tree. Here's a simplified example...
|
We create a Node struct, which contains some data, as well as a pointer to the next item in the list. |
|
Each Node links to the next Node in the list. |
C++ also has smart pointers available, which handle the memory allocation and deallocation automatically. Usually it's better to not be manually doing memory management in your programs, so these objects would be preferred in the real world. However, you may still need to work with traditional pointers in future classes, so I tend to focus more on using those.
shared_ptr https://cplusplus.com/reference/memory/shared_ptr/ Shared pointers are meant for spaces in memory that will be pointed to by multiple pointers. The memory allocated at that address stays active up until the last pointer is destroyed - that triggers the deallocation of the memory once nobody is pointing to it anymore. |
unique_ptr https://cplusplus.com/reference/memory/unique_ptr/ With a unique_ptr, we create a pointer that is the only "owner" of the memory block it points to. We can move around "ownership" of that address, but only one pointer may point to it. When the owning unique_ptr goes out of scope and is destroyed, it will automatically free the space allocated at the memory address it owns. |
Example code: (repository)
Archived videos and class lectures: