Are pointers just silly?

I understand that pointers store a 0x number that is the location to another point in memory. You can store the &address of something and use the *data pointed by that address to access memory. But isn't it a bit wasteful?

If I have a variable declared somewhere, I assume that I could just reference that variable directly with its identifier anywhere in the code. Using memory to store its location and then access it would just take up more RAM.

Then comes the problem of scope. A variable declared outside the current class or function etc is hidden from others unless it is a global variable. If I want access to something out of scope, use a pointer in scope to get it. But if I needed to do that, why wouldn't I just broaden the scope of the variable? Make it global if I need to?

To me it just seems silly. The scope system seems to require pointers when I could just access a variable. So, all that to say; why use pointers?

Thanks.
I understand that pointers store a 0x number


pointers do not store any kind 0x number.

why use pointers?

to dinamicaly store objects on the heap.
to reduce the size of your class.
they are useful for event-driven programing
C++ supports pointers as a legacy feature. Nobody says you must use pointers.
That said, pointers aren't necessarily used for something as simple pointing to a variable.
They can be used for dynamic memory allocation, although that's also superseded by Containers.

Anyway, here's an example of pointer abuse:
http://nehe.gamedev.net/tutorial/model_loading/16004/
He loads the whole model file into memory, then by using a pointer, which is repeatedly casted, he extracts exactly the data he wants.

@ codekiddy: I think he's referring to the fact that when you std::cout a pointer, it'll show a number in base 16 (and so, prefixed with 0x).

Edit: small typo.
Last edited on
They are actually very useful.
Just one way is, if you are creating objects and numbering them as you go, you can create an array of pointers for the objects and then use its position in the array to determine which object you want to use.

Just an example:
Lets say you were creating flowerpot objects, and you wanted to calculate the volume of each one. Seems easy right? Here's the catch. Assume that you have no idea how many to make. You need to ask the user. Without pointers, there is no way to increment object names or anything like that. So what you can do is, as you create the objects (which will all have the same name because they are in a loop), you create a pointer for each one that gets put in an array. Then, when you want to use it, you can use something like flowerPotArray[i]->GetVolume, i being the number of the flower pot.

Not sure if I made that clear at all, but there is one example.
closed account (zb0S216C)
Another valuable use of the pointer is iteration. You can't iterate with a reference, can you? Sounds as though you haven't had much experience programming with pointers. If you did, you won't be saying "their silly.", and this thread would've never seen the light of day.

Wazzak
Let's say that I have a structure or class of a particular type that I want to send (accross a network, or accross a process). The middleman may not know of that type of structure. In order to still send it, we just provide the middleman with the pointer and the size. With that information the data can be packed, sent, and unpacked without having to modify the source code of whoever did the actual delivery.
^ No if your class has pointers as members.
to reduce the size of your class.
Just to make it clear, it is referring to the space occupied in the stack.

Also, relationships.
The scope system seems to require pointers when I could just access a variable.


That is incorrect. It is common to need to access a value in memory for which you have only a pointer to; no local variable.

C++ supports pointers as a legacy feature.

Oh Catfish, seriously? A legacy feature? That's just not the case.
Last edited on
Sorry about that, I was spazzed out.
Let's not forget dynamic polymorphism achieved via pointer to base class. (And about a dozen other useful things to do with pointers.)
I have a screwdriver. Is it just silly for hammers to exist? :)
Moschops,
ahahahhhaahahh
Topic archived. No new replies allowed.