Pointers
Well, imagine you have a very large tree that you have to pass to a function. Without pointers, you would have to pass the whole gigantic structure to the function, which takes a lot of time and stack space. Or you can pass it by pointer or reference (let's not get into the itty-bitty differences right now), which is just putting one int-sized variable onto the stack or into a register, which is neglible both spatially and temporally.
@Demus79: wait.. wait.. are you saying that I could make something like an array that instead of having a size of "X" elements, I can keep increasing it's size as I need it to be larger?
Have a look at some of the tutorials on this site, like the one
here and here are some of the best I've seen out there. Basically though as was said by others, a pointers store a memory address to data rather than data itself. They become very useful when working with large chucks of data like arrays, vectors, or later if/when you get into the OO aspect of the language, passing actual objects around. So rather than pass a 200k object(don't ask me what would be that size I'm just making up a scenario) anyway, so rather than pass that huge object around we can pass a pointer that's the size of an int. Pretty cool huh..
Lol this has to be one of my dorkiest posts yet... :P
here and here are some of the best I've seen out there. Basically though as was said by others, a pointers store a memory address to data rather than data itself. They become very useful when working with large chucks of data like arrays, vectors, or later if/when you get into the OO aspect of the language, passing actual objects around. So rather than pass a 200k object(don't ask me what would be that size I'm just making up a scenario) anyway, so rather than pass that huge object around we can pass a pointer that's the size of an int. Pretty cool huh..
Lol this has to be one of my dorkiest posts yet... :P
Quote:Original post by Steveosaurous
I understand that pointers are used to hold the memory address of variables and such...
But I fail to see how this is useful in programming with the exception of memory management
As I can allocate and de-allocate memory at will
I don't see what pointers can do that variables can't
Aside from low-level memory stuff and such, pointers are useful for what ToohrVyk mentioned: reseatable references. For example, let's say you are designing a 3D modelling suite. You can select meshes, and when you press "M" you change the selected mesh's material. How are you going to do that? When you're writing the program, you don't know what the selected mesh would be, so you can't use variables. When you write:
Mesh sphere;
Mesh cube;
There are 2 objects, sphere and cube. Using variables, you can reference the sphere with "sphere" and the cube with "cube". When you want to set the material for sphere, you need to write "sphere.SetMaterial()". Same for cube. You need to know which object you want to use when you write the program. The "selected mesh" is something more abstract, meaning that it can refer to any of the meshes that are currently on screen. It could be the sphere. It could be the cube. It could be none. What data type you'll use that will allow you to know which is the "currently selected mesh"? You can store all the meshes in an array and reference the selected mesh with an index, but this isn't very flexible(although it can be used). When you use pointers, things become simpler(if you know what you're doing):
(1)Mesh* selected_mesh;
(2)selected_mesh=&sphere//select sphere
(3)selected_mesh->SetMaterial();//sets material of sphere
(4)selected_mesh=&cube//select cube
(5)selected_mesh->SetMaterial();//Exactly the same code with (3), only now we set the material of cube!
Via "selected_mesh", you can now reference any mesh that exists. By doing "selected_mesh->SetMaterial()", sets the material for that mesh. The difference with C++ references is that references are initialized only once, while the object a pointer "points" to can be changed as many times as you want during the program.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement