Pointers vs Variables

###Just a note: I could be completely wrong in my understanding

#Pointers vs Variables

###Variables

So my understanding of pointers is used for better performance. When you declare a standard variable, it stores it’s value type and not the address at the point it was created. So when you go to access it, it’s possible you can grab that value, however the address in memory could be different. You can grab the address at it’s current location by doing &variable but it’s not as static, and you would only do that if you were setting it to a pointer.

###Pointers

When you create a pointer, it not only stores the value, but also an address that the variable name and value is stored in the address. It’s more of a static address and isn’t lost when you try to access via the deconstruction of the pointer *pointer. Since the memory address isn’t lost during time of creating the pointer. We can directly go it it’s value, without searching the memory for that particular variable value when we go to deconstruct it.

When to use Pointers vs When to use Variables

So when a variable doesn’t take much space in memory, say something like a 4 byte integer or a string. With memory allocation of those variables, it would barely be noticeable at run time. So variables for small bytes of data. However with thing’s such as classes with a lot of data and bytes attached to it. I would say re allocating that size, would slow down the performance. So it’s better that we store it in a pointer, so instead of just grabbing it’s value which could be re allocated in memory. It’s better to go to that static memory address and deconstruct it, to it’s value.

Summary

This is my understanding of pointers and when and why to use them. I could be completely wrong, and have miss-interpreted what I’ve read. If anyone has any comments to help clear up things for me, please reply and let me know!

Thanks!

There seems to be a misunderstanding here:
Pointers do not store a value. ONLY an address.

If you were to do the following:

int* pointy; //create a pointer that stores an address
*pointy = 32; //dereference the pointer and store 32 at the location pointed at

you could by fine.
However: Since you have no idea where the pointer is pointing, you could be overwriting something else in the RAM storage, mabye part of the content of your VeryLargeObject you worked on just 2 minutes ago. You could be lucky and actually write to a free memory spot. Or you hit the nullptr and your program crashes.

Doing this instead:

int num; //create a variable, that has an address
int* pointy = # //get the address of the above variable (not a new address, just the one from the variable)
*pointy = 32; //dereference the address and set the value to 32, therefore setting “num = 32”

everything is fine.

Regarding run-time performance, imagine a function call:

A function func(longlonglonglongfloat num) {…}
func(pi);

The function will COPY pi to another location and work with it. real world pi is without end, this pi only uses let’s say 1Gb of you RAM instead.This is not good for your run-time.

A function: func(longlonglonglongfloat**&** num {…}

func(*pointerToPi) // you have to declare the pointer first though, prefer the next line:
func(&pi) //same, but faster since you don’t have to declare a pointer first.

on the other hand copies the address of Pi, 32 or 64 bit long. That doesn’t take much time, and by dereferencing the value in the function you still have access to it, without heavy impact to run-time (except for the calculation time when you actually use pi later, but this is the same for both approaches).

1 Like

Heya! Thanks for the reply! Yeah i did more research and saw the video that covers this haha. But the explanation you give really helps this make alot more sense! I really appreciate it, thank you!! :slight_smile:

1 Like

what is different between *(pointer) and &(reference) both of them get address and return value?

as I understand it :
*(pointer) has an address and pointing to another one like *(pointer)->&(reference)
&(reference) the address itself for example : &(pointer) the address of the pointer

&(reference) is not related only *(pointer) example:
**int k=30; int &j=k; it’s right order.
I can pass the variables by *(pointer) as well by &(reference)
and I am interested in which is better practice?

1 Like

normally when I pass the variable to a method I send it as reference then in method I receive it as pointer like :
int* ptr = 34;
somemethod(&ptr)

you don’t need *(pointer) at all. you can write function implementation:
somemethod(int & ); like this.

call the function:

int prt=34;
somemethod(ptr); this result is the same as pointer and you don’t need extra symbol in front of variable I think it’s very convenient.

but it must say that there are some restrict for pointer as well for reference

simply spoken: a reference is a little safer to use, but has less functionality.

  • If you want to access the value, you have to dereference pointers like so: " *pValue = 32; "
    otherwise you get compiler errors.
    Thats not needed with references and makes it safer to type, and less error-prone.
    There’s more to it, but I cannot explain that well enough.

  • On the other hand:
    If you plan on changing the address in your function
    (e.g. changing your render target, switching between widget layers),
    a pointer might prove neccessary, since you cannot re-address a reference at all. Once you initialize a reference it does not cange its address.
    That’s also important for multithreading. Furthermore References can not point to NULL.

EDIT: Thanks Levani, i screwed up here, don’t read it. Updated Tue Jul 25 2017 15:39

e.g. this is not possible:

int A = 32;
int B =16;
int& ref = &A;
ref = &B;  //compiler-error, not possible 

What i meant was this:

int A = 32;
int B = 16;

int& Cref = A;
/*ref is a sysnonym for A, ref itself IS practically A and can be used 
like an integer-variable, although it is a reference*/

Cref = B;
/*changes the "content" of ref, or correctly spoken changes the content 
of the variable A, which is represented by its synonym ref*/


//Something like the following is impossible with references:
int* pointy = &A;
std::cout << "Adress: " << pointy << " Value: " << *pointy << std::endl;

pointy = &B;
std::cout << "Adress: " << pointy << " Value: " << *pointy << std::endl;

//there is no implemented way to change the address of a reference

Check out this post on Stackoverflow for more/better information (especially thequestion and the first and third answer):

No, you forgot that reference need not any symbol when it takes the address:

ref = &B; //compiler-error, not possible

because you must write _ref=B;
and it would not be error from compiler

You dont need the underscore. The Underscore is just a notation-thing to clarify that this is a reference.
Other Notations use different prefixes. (I don’t think that the Unreal Engine forces you to use an underscore as a prefix)

int& _Cref = B; 
int& Cref = B;
int& Obj_Cref = B; //if you want to annoy people you could even go with this

thank you for clear

Privacy & Terms