简体   繁体   中英

Can I have an ELI5 for references and pointers and when to use them?

I've been learning coding for quite a while now but still cannot understand References and Pointers. Every answer I've searched is way too complicated for me.

When we're calling a variable to be used, for example

int32 y = 1;
int32 x = 2;
int32 z = y + x;

1) What exactly is happening in "z"? Is it calling "y" and "x" by reference, by pointer or just calling them by variable?

In this code I'm currently learning

FString Log = TEXT("Hello");
FString* PrtLog = &Log;
Log.Len();
(*PrtLog).Len();
PrtLog->Len();

2) What is going on here? Is "PrtLog" a reference or a pointer?

3) The lecturer said *PrtLog is "dereferencing" PrtLog. Does that mean the reference for PrtLog is removed? Whats the difference between * and ->

4) Why do we even need a reference or a pointer if calling a variable is just as fine?

5) Why do some people claim 90% of variables will be using references and pointers in higher levels? Are they beneficial in any way? If we just call by variable, isn't it simpler and faster?

Sorry if this is too many questions. I can't get an answer I'm able to understand anywhere on references and pointers so I'm really confused.

1) What exactly is happening in "z"? Is it calling "y" and "x" by reference, by pointer or just calling them by variable?

None of those variables are pointer or references. They're just..variables. x and y are used variables. operator+(x, y) .

2) What is going on here? Is "PrtLog" a reference or a pointer?

PrtLog is a pointer, you can see this by looking at its type declaration:

FString*

Clearly it's a pointer to a FString .

The confusion might arise because of the = &Log; . In this part of the code & is the address-of operator required to get a pointer to Log . & only means reference when it's part of a type, Log here is a variable, not a type.

3) The lecturer said *PrtLog is "dereferencing" PrtLog. Does that mean the reference for PrtLog is removed? Whats the difference between * and ->

Dereferencing is just an unfortunate name in this case, it means to get the "thing" that the pointer is pointing to. A FString* is a pointer pointing to a FString so dereferencing such a pointer would yield a FString .

The difference between * and -> is that -> is a shorthand for: (*pointer). , or "Dereference pointer and access its member`.

4) Why do we even need a reference or a pointer if calling a variable is just as fine?

There are a few possible reasons you'd want to use a pointer or a reference. For example, to refer to an object but not copy the object itself.

5) Why do some people claim 90% of variables will be using references and pointers in higher levels? Are they beneficial in any way? If we just call by variable, isn't it simpler and faster?

Who claims this? I don't have the numbers but this doesn't seem accurate. Surely they're both very useful constructs but it very much depends on the project if they're used in those numbers.

x and y are called lvalue expressions (usually shortened to lvalues ). That means they correspond to memory locations. The context of the expression determines whether a value is written to the memory location, or a value retrieved from the memory location.

In the code x = 2; then a value is written to the location named by x . In the code x + 2 , a value is read from the location named by x .

PrtLog is a pointer because it was declared with a pointer declarator. The question of why someone would use pointers is answered here: Why use pointers?

"dereference" means removing a level of indirection from a pointer expression. A pointer points to a memory location. The result of dereferencing a pointer is an lvalue expression corresponding to that memory location. There can be multiple levels of this. a->b is equivalent to (*a).b if a is a pointer.

A pointer is just a number. This number corresponds to where in your computer's RAM the value of the corresponding variable is stored.

int theAnswer = 42;
int *pointer = &theAnswer;

std::cout << pointer << '\n';
/*
 * this will print where in memory `theAnswer` is stored,
 * it'll just look like some random number. Try it out!
 */

So in your example, (2) PrtLog is a pointer. A reference is also a pointer, but C++ sort of 'hides' that it is one.

(3) Remember that a pointer is just a number. So if you want to work with the pointer's value, as opposed to doing math with the pointer itself (which you shouldn't do ig), you need to somehow 'follow' that number to where the value is stored. This is what dereferencing does. It 'follows' the pointer to get its value, and allows you to do things like call functions or modify it.

// make an integer variable
int theAnswer = 42;
int *pointer = &theAnswer;

std::cout << "Original: " << theAnswer << " Pointer: " << *pointer << '\n';
// prints "Original: 42 Pointer 42"
*pointer = 41;
std::cout << "Original: " << theAnswer << " Pointer: " << *pointer << '\n';
// prints "Original: 41 Pointer 41"

(4) Sometimes you just can't get access to the original, like with runtime memory allocation, or if you want to make a method to mutate a variable, etc. In c++ a lot of this is hidden in the standard library with things like references and classes, but it does still come up occasionally. Although, if you're working with raw pointers (as opposed to references or RAII classes) in C++, you should have a good reason for doing so, leaking memory is really easy.

// you can only access this variable though the pointer
int *dynamic = new int;
*dynamic = 42;

std::cout << *dynamic << '\n';

delete dynamic;

(5) This seems to be the same question as 4, but I may be interpreting it wrong.

I hope that's clear. If you have any other questions feel free to comment and I'll try my best to answer them, I'd be happy to help a fellow Benjamin out!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM