Learning C++: Part 6: Pointers

Learning C++


Pointers

C++ is considered a “systems-level” programing language because of its ability to work directly on the memory of the device on which it is running. Many complex applications are written in C++ for this very reason.

Part of how C++ works with memory is through a concept called pointers. In system programming languages, a pointer is a variable that holds not a value but the location of some value. It can be thought of through the metaphor of a letter with the address on the envelope and the message inside of it.

Normally, when working with variables, the message in the envelope is important. When working with the “letter,” its value is changed through giving it a new value or in seeing what value it already holds.

Pointers work through holding the address on the envelope. They are not concerned with the message itself, but what the address is. Pointers are named as such because they point to a place.

All variables exist at a location when created. This is its address. Using a single ampersand (&) in front of a variable will return its address instead of the value it holds.

Types of Pointers

Just like variables and functions, pointers also have types. That is, this lets C++ know what type of data it is pointing at.

Pointers are created in C++ through using the asterisk (*) in front of the name of a variable. This tells C++ that the variable should hold the location of some data of its type.

Why are pointers important?

While being able to retrieve and examine the location of some variables may not seem as important at face-value, it gives access to many complex data types because of the ability to point to new locations in memory and to build noncontiguous data entries.

Normally, when something like an array is created, a section of memory is cleared and the array takes up that location. For however long it is, that is the full block of memory. The entire thing is one long, continuous section in memory.

When working with large sets of data, this is extremely impractical. Taking up large chunks of memory is extremely inefficient for most projects. What would be more helpful would be to make something that could change sizes and take up memory locations in different locations at the same time. Something that could point to the next data in the set.

Structs

Starting with C, it has been possible to add new data types through the keyword struct. Through defining what type of existing data should be inside of it, a struct can be thought of as the blueprint for a new type of data type.

Through combining pointers with structs, it is possible to create a new data type that has a pointer to the next item in a list. Through creating new data, a list of different entries can be created that point to each other in turn. This serves as the basis for a concept called a linked list.

To access data in a struct, its members are part of the dot-notion. A period separates the name of the member from that of the name of the variable holding the data following the blueprint of the struct.

To create some data of a certain type, the new keyword can be used. Based on the type of data of the variable, this create a new entry of the same type.

This is a major change between C and C++. Instead of needing to allocate and manage memory more directly, the new keyword can be used as a shortcut for “make a new thing matching this existing thing’s type.”

Because the new keyword works with addresses, the value set when using it is the location of the new data. When used with a variable, it is the message of the new “letter.” When working with a pointer, it is the new address.

To work with the data of a variable through a pointer, the “pointing right” arrow is used. With a minus and greater-than sign, “->” the variable visibly points to the place of the new variable.

Play with the example on Repl.it!