###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!