We know that variables in a computer program are used to label data with a descriptive identifier so that the data can be accessed and used by that computer program.
In Python every single thing is stored as an object. Hence, a Python variable is actually a reference to an object that is stored in memory. Hence, each Python variable requires two memory locations: one to store the reference, and the other to store the variable value itself in an object.
In C++ the value of each variable is stored directly in memory without the need for either a reference or an object. This makes access faster, but it is one of the reasons we need to declare each variable because different types take differing amounts of space in memory!
But, we can also identify the memory location of the variable by its address. In both Python and C++, this address may change each time the program is run. In C++, the address will always look odd because it will be the actual memory address written in a hexadecimal code which is a base 16 code like 0x7ffd93f25244. In Python it is implementation dependent, it is sometimes a hexadecimal code and sometimes just a count or another way to reference the address.
In both Python and C++, variables are stored in memory locations which are dependent upon the run itself. If you repeatedly run the above code in either C++ or Python, you may see the location change.
As suggested above, in Python, it is impossible to store a variable directly. Instead, we must use a variable name and a reference to the data object. (Hence the arrow in the image.) In C++, variables store values directly, because they are faster to reference.
References are slower, but they are sometimes useful. If in C++, we want to create a analogous reference to a memory location, we must use a special data type called a pointer.
When declaring a pointer in C++ that will “point” to the memory address of some data type, you will use the same rules of declaring variables and data types. The key difference is that there must be an asterisk (*) between the data type and the identifier.
However, the first declaration is preferable because it is clearer to the programmer that the variable is in fact a pointer because the asterisk is closer to the variable name.
Now that we know how to declare pointers, how do we give them the address of where the value is going to be stored? One way to do this is to have a pointer refer to another variable by using the address-of operator, which is denoted by the ampersand symbol, &. The address-of operator & does exactly what it indicates, variableType varN; // a variable to hold the value namely it returns the address.
Once you have a C++ pointer, you use the asterisk before the pointer variable, to dereference the pointer, which means go to the location pointed at by the 3.
Compiling and running the above code will have the program output the value in varN, what is in ptrN (the memory address of varN), and what value is located at that memory location.
The second cout instruction is a disaster because (1) You don’t know what is stored in location 100 in memory, and (2) that location is outside of your segment (area in memory reserved for your program), so the operating system will jump in with a message about a “segmentation fault”. Although such an error message looks bad, a “seg fault” is in fact a helpful error because unlike the elusive logical errors, the reason is fairly localized.
Like None in Python, the null pointer (nullptr) in C++ points to nothing. Older editions of C++ also used NULL (all caps) or 0, but we will use the keyword nullptr because the compiler can do better error handling with the keyword. The null pointer is often used in conditions and/or in logical operations.
The following example demonstrates how the null pointer works. The variable ptrx initially has the address of x when it is declared. On the first iteration of the loop, it is assigned the value of nullptr, which evaluates to a false value; thereby ending the loop:
Helpful Tip: The null pointer becomes very useful when you must test the state of a pointer, such as whether the assignment to an address is valid or not.