Is it good to have pointers for majority of variables?

Apparently pointers are super handy in increasing compilation speed because there isn't the need to search for each variable one by one, just knows the address and wa la. So, is there anything wrong with having pointers for almost all of your variables?
Apparently pointers are super handy in increasing compilation speed because there isn't the need to search for each variable one by one, just knows the address and wa la.


I don't understand what you mean by this. The pointers have to point to something anyway, so you aren't getting anything for free, if that's what you think.
If you mean replacing all of your static variables with dynamically allocated ones, I would recommend against it. Besides being strange to read and required you to remember to delete everything, that would just transfer the variable management from compilation-time to runtime. The storage space required would be higher, too, as each variable would have its address store on the stack and its value on the heap, rather than simply having the values on the heap stack. Access would also probably be at least a bit slower since every variable access would require going through a layer of indirection.
Last edited on
@firedraco I read that when your program searches for a referenced variable it has to look through each variable one by one until it finds it. Apparently if you use pointers it can quickly reference it. Was a web source, so let me know if that's false.

@Zhuge Ok thanks, I understand that pretty well.
@firedraco I read that when your program searches for a referenced variable it has to look through each variable one by one until it finds it. Apparently if you use pointers it can quickly reference it. Was a web source, so let me know if that's false.


That is not the case. Static variables (as they are called), are replaced with their address at compile time so that when the program is run; there is no 'searching' involved. Even if what you said was true, the pointer value would have to be stored somewhere, and thus the same cost would be incurred anyway.

Anyway, pointers are usually slower since they will involve a dereference (look at the pointer to determine where to look in memory) whereas static variables do not.
Topic archived. No new replies allowed.