Could pointers in programming be compared to shortcuts?

 


Could pointers in programming be compared to shortcuts?

Pointers define an access mechanism. You might have a pointer which points to something else, then to another thing and down a path to what you want:


a.b.c.d.e


So you can pass around the pointer to a, but to get to e from there you must follow the path. That is actually a bad idea because you ‘bake’ the path into your program.


Instead you could directly pass the reference to e around. But notice what is going on, when you pass a reference, you are giving access permission. It is exposing something. A reference is a name. If I know your name, I can call you. If I don’t I can’t. It might not be quite so clean cut in human interaction because I can say ‘hey you’, but in computing names are that clean. A name is a reference to an entity – if you have it you can access that entity, if not, you can’t.


Now a pointer in many computing usage is just a raw memory address, but to form a reference to something you need to know at least how big it is, and what types of elements it contains. But pointers don’t do that. So pointers are very dangerous and in fact weak.


A reference can also be thought of in two levels. The system view is to see it as a block of memory, thus a pointer, length, and type of contents. This is thinking about the storage itself. But really at most levels we are interested in what is being stored. In a library we are interested in the books, not the shelves. In a zoo we are interested in the animals, not the cages (enclosures) – how disappointing is it to look at an enclosure and not be able to see the animal, or to see a sign, that animal is not in at the moment.


So beware of the weak definition of pointer as just a memory address, because they can expose things – in the shortcut sense – that you don’t want exposed. They can be a backdoor that subvert security. Note that in the path a.b.c.d.e any of the intermediate steps can do a security check and reject the access. But if you just pass a direct reference to e you may well be subverting those checks.


Pointers as commonly defined are a very bad idea to use in programming languages. They are weak and you need more information to define a robust and strong system.

Plus récents Le plus ancien