mirror of
https://github.com/LukeHagar/unicorn-utterances.git
synced 2025-12-09 12:57:45 +00:00
Add Pointers and References Article
This commit is contained in:
153
content/blog/pointers-and-references-cpp/index.md
Normal file
153
content/blog/pointers-and-references-cpp/index.md
Normal file
@@ -0,0 +1,153 @@
|
||||
---
|
||||
{
|
||||
title: 'Pointers and References in C/C++',
|
||||
description: 'An overview of how pointers and references function in C/C++',
|
||||
published: '2020-05-20T09:40:00.000Z',
|
||||
edited: '2020-05-20T09:40:00.000Z',
|
||||
authors: ['seanmiller'],
|
||||
tags: ['computer science', 'cpp'],
|
||||
attached: [],
|
||||
license: 'cc-by-nc-nd-4'
|
||||
}
|
||||
---
|
||||
|
||||
# Pointers and References
|
||||
|
||||
Every new C/C++ programmer will eventually reach the point at which they are forced to work with pointers and will undoubtedly realize that they extremely dislike using them because they are a little complex. Today, we'll be looking at what pointers are, deconstructing their usage, and hopefully, making the usage of pointers easier to grok.
|
||||
|
||||
|
||||
## What is a Pointer? {#what-is-a-pointer}
|
||||
|
||||
|
||||
|
||||
A pointer is simply a variable or object that instead of holding a value, holds a memory address to another spot in memory. You will commonly see a pointer being most recognizable by their declaration including the **\*** operator, also known as the **dereference operator**. This operator is called the dereference operator because when you try to access the value that the pointer is referencing, you have to use the **\*** operator to "de-reference" the value. Which is just a fancy way of saying, "go to that reference".
|
||||
|
||||
Here's an example:
|
||||
|
||||
```cpp
|
||||
// Defines int variable num
|
||||
int num = 12;
|
||||
|
||||
// Declares pointer p
|
||||
// Ignore the '&' symbol here, we'll discuss that next
|
||||
int *p = #
|
||||
|
||||
// Prints out the memory address that p is pointing to
|
||||
cout << p << endl;
|
||||
|
||||
// Prints out the memory address of num
|
||||
// Again ignore the '&' symbol here, we'll discuss that next
|
||||
cout << &num << endl;
|
||||
|
||||
// Dereferences the pointer p
|
||||
cout << *p << endl;
|
||||
|
||||
// Prints out the value num
|
||||
cout << num << endl;
|
||||
```
|
||||
|
||||
This should print out something like...
|
||||
|
||||
```
|
||||
0xffffcc14
|
||||
0xffffcc14
|
||||
12
|
||||
12
|
||||
```
|
||||
|
||||
As you can see, the pointer p holds the memory address of num, and when you use the dereference operator, the value at num is printed out.
|
||||
|
||||
Pointers can also get a lot more complex and must be used in certain situations. For example, if you put an object on the heap (Check out my article on Virtual Memory to learn more about heap memory) then you will have to use a pointer because you can't access the heap directly. So, instead of having a pointer to an address on the stack, it will point to an address on the heap. You might even find yourself using double or triple pointers as you get more used to them.
|
||||
|
||||
|
||||
## What is a Reference? {#what-is-a-reference}
|
||||
|
||||
In simple terms, a reference is simply the address of whatever you're passing. The difference between a pointer and a reference lies in the fact that a reference **address** where a value of some sort is being stored, a pointer has a value at an address different from the one it’s pointing to. I like to consider the **&** operator the "reference operator" even though I'm pretty sure that's not actually what it is called. I used this operator in the last example, and it's pretty straightforward.
|
||||
|
||||
```cpp
|
||||
int num = 12;
|
||||
int *val = #
|
||||
int **doublePointer = &val;
|
||||
|
||||
cout << "Address of num: " << &num << endl;
|
||||
cout << "Value of val" << val << endl;
|
||||
|
||||
// Pointers have memory addresses too!
|
||||
cout << "Address of val" << &val << endl;
|
||||
|
||||
// Prints out every phase of the double pointer
|
||||
cout << &doublePointer << " : " << doublePointer << " : ";
|
||||
|
||||
cout << *doublePointer << " : " << **doublePointer << endl;
|
||||
```
|
||||
|
||||
The output will look something like this...
|
||||
|
||||
```
|
||||
Address of num: 0xffffcc1c
|
||||
Value of val: 0xffffcc1c
|
||||
Address of val: 0xffffcc10
|
||||
0xffffcc08 : 0xffffcc10 : 0xffffcc1c : 12
|
||||
```
|
||||
|
||||
As you can see, all the **&** operator does is gives you the memory address at its specific spot in memory. I also included a small example of a double-pointer which is just one more layer of abstraction then another pointer. You can see how the memory addresses line up.
|
||||
|
||||
Here’s what this looks like in memory with more easily understandable addresses in a “0x…” format.
|
||||
|
||||
****
|
||||
|
||||
## Pass by Reference vs. Pass by Value {#passing}
|
||||
|
||||
This is another more complex topic that we as programmers need to be aware of in almost all languages - even languages without pointers. The idea of the two all stems from functions, or methods, and their parameters. Whenever you pass something to a function, does the original variable/object gets updated in the function as well as outside of it, or is it hyperlocal and it just creates a copy of the original parameter. "Pass by reference" refers to when the parameter is changed both within the function and outside of it. "Pass by value" refers to when the parameters are merely a copy and have their own memory address.
|
||||
|
||||
The primary difference between the two is what happens when you change values. If you pass by reference and update the property, it will update the original variable that was passed as well. However, if you pass by value and update it in function, it will not impact the original variable.
|
||||
|
||||
The neat part of C++ is that you can control whether something is passed by reference or passed by value, but rather than try and explain it further let's look at an example.
|
||||
|
||||
```cpp
|
||||
// Pass by reference
|
||||
void passByReference(int &num) {
|
||||
num += 2;
|
||||
}
|
||||
|
||||
// Pass by value
|
||||
void passByValue(int num) {
|
||||
num += 2;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
int num1 = 0;
|
||||
int num2 = 0;
|
||||
cout << "Original values: " << num << endl;
|
||||
|
||||
// call passByReference()
|
||||
passByReference(num1);
|
||||
|
||||
cout << "After passing by reference: " << num << endl;
|
||||
|
||||
// call passByValue()
|
||||
passByValue(num2);
|
||||
|
||||
cout << "After passing by value: " << num << endl;
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
The output of this looks like...
|
||||
|
||||
```
|
||||
Original values: 0
|
||||
After passing by reference: 2
|
||||
After passing by value: 0
|
||||
```
|
||||
|
||||
As you can see, when passed by reference the local value is changed but when it is passed by value it is not even though they both perform the same operation. In languages outside of C++ — like Java for example — doesn't give you control of whether you pass by value or reference; Some types of objects or variables are passed by reference and some aren't. For example, Java collections are passed by reference, but all other variables and objects are passed by value.
|
||||
|
||||
This gets confusing after a while if you're not paying attention to your outputs. In fact, Python gets even more confusing, but that's a topic for another day; be sure to sign up for our newsletter to see when that lands 😉
|
||||
|
||||
|
||||
|
||||
## Review/Conclusion {#conclusion}
|
||||
|
||||
Pointers and References are extremely important in your day to day work in languages like C/C++. C++ gives you a lot of manual control with the most common being memory. Knowing how each one of your pointers or variables are stored will help you write code faster and more efficiently. Knowing also how passing parameters to your functions works will make your life **so** much easier.
|
||||
BIN
content/blog/pointers-and-references-cpp/memory.png
Normal file
BIN
content/blog/pointers-and-references-cpp/memory.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 8.6 KiB |
Reference in New Issue
Block a user