Pointer

Write your question here.
Hello. Using pointers to a variable is faster than accessing a variable directly. As I understand it, when accessing a variable directly, it first goes to the address of the variable, then it is copied to a new address. When using a pointer, you immediately jump to the address of the variable. Do I understand correctly?
Just for accessing a variable, no, what you've said isn't true.

Are you talking about passing objects to a function? Because if so, then what you've written makes a little more sense. If you pass-by-copy into a function, then it's often quicker to copy a pointer, than it is to copy a larger or more complex object. This is often done in C.

However, in C++, a better solution is to pass-by-reference.
As I understand it, often a pointer can be greater than the value of a variable?
executable code does not have any variables. it only has addresses. even assembly language has variables, but the actual executable instructions are just raw addresses. And there is no extra copy from memory to memory. The copying, when it needs to happen, is memory to register; for example on intel to access an array location (from what I recall, and its been a while) you have to copy the address of the array's first location to a register and can then use the register to address the array in tandem with another register.

A pointer IS a variable, in c++. Simply having a pointer adds to your executable code more bloat that has to juggle the value of the address, as if it were an integer, because the program now has to store that information and in many cases pointers change value frequently and it has to track all that. To do something with a pointer in c++, you now have to go to the memory location of the pointer variable, get the integer, pull that back and then go to the location in memory that the integer represents, pull that back ... its an extra layer of work for the CPU to have to do, extra instructions, and while the compiler MAY be smart enough to bypass this extra work some of the time, its nothing you can second guess on when it can or cannot do that without doing some study on your compiler's behavior.

references try hard to bypass the above extra layer. The compiler knows that a == b, and it knows what b is, so instead of moving b into a and the using a, it just used b directly if possible. Its similar to the above, but the compiler has a better chance of doing less work with references in my experience. References do not change, so it does not have to track them, it just needs to look up what they originally refer to and inject that at compile time.

The key thing to take away here is that a pointer IS a variable. If you understand the implications of that statement, you can see why a pointer is NOT faster than going directly to the data.

So how would a pointer make code faster, then?
Lets say you had a big, fat object with name, address, government ID (ssn or whatever your country uses), gender, date of birth, spouse info, and all your medical history complete with xray image data. Now lets say you had that for, oh, 10 million people in your large city hospital database. You want to sort it by last name.
you can do a swap() of this thing, copying possibly thousands of KB around every operation, or you can make a container of pointers and sort the pointers by the pointed-to last name, swapping only the pointers, which are about like swapping an integer and can even be done in pure cpu registers without a real temporary. See how that is significantly faster to use the pointer? Even though the pointer added a variable and a layer of access to your data, it gave you a speed lift because you avoided a bunch of unnecessary copying of a ton of data. That is the kind of thing pointers are very good at doing.

Thank you very much! The level of optimization affects the direction of copying: register - memory or memory - memory?
I would think you mean the difference when passing (to a function) by reference or by value? Well, the simple rule of thumb is of course: complex data (more than a single int) by reference and simple data like an int by value.

The level of optimization affects the direction of copying: register - memory or memory - memory?
It doesn't make sense to think too much about this kind of optimization. The compiler usually chooses the best model for this. And speed is in most cases not relevant.
Thank you very much! The level of optimization affects the direction of copying: register - memory or memory - memory?

most platforms do not allow memory to memory copy, most systems must pass through the cpu via x to register, register to y but this gets wonky because most cpus have a cache between ram and registers which is faster than ram access. Then you go off into la-la land and can start talking about why (schoolroom style) trees and linked lists can suck due to scattering the nodes across memory pages and thrashing the cache pointlessly, which the os memory manager tries to assist with but can't ensure it esp over a long run period with loads of data... it quickly becomes rather complex and trying to second guess it is futile.

As Coder 777 said, its not important to dig too deep here. The critical point from the wall of text is that using an extra pointer does not save you anything, and that references are similar to a pointer but usually (always?) do not add any extra steps while pointers (frequently?) do. The other info is a high level why to this statement. Its also useful to take away the idea of when a pointer does help: the sort example would be really good to understand the difference between sorting pointers to the thing vs the actual fat data.
Last edited on
Topic archived. No new replies allowed.