• Advertisement
Sign in to follow this  

Pointers

This topic is 4216 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
I do believe so. Now, don't really take my word for it. I still don't fully understand these little guys myself. They are very useful though. Pointers can be described like this:(In my own opinion) A useful segment in code used to store data for later use. I hope that clears your understanding. But I could be wrong, that is more of a guess.

Share this post


Link to post
Share on other sites
Pointers are Very useful; if used at the right times but can quickly turn otherwise good code into speggehti(sp?) code.

One example of good pointer use is page buffering (you know using two images for the display, one gets updated while the other gets rendered).

In the early days, they used to copy all of the backbuffer's data to the front buffer and that, as you know, took up alot of memory and processing. Then some one came up with the Idea of using a pointer, instead of doing all that copying just change the pointer (which is only a few bytes to [at the time] huge kilo bytes) . I don't know when pointers were 'invented' but Id guess sometime around then.

As for memory management, they can be very useful. One method would be to have your memory manager object keep somesorta list of all things that need deletion etc with a pointer, and this saves memory with minimal overhead cost. Exactly what memory management systems strive to do.

Share this post


Link to post
Share on other sites
Pointers allow us to design good data-structures and algorithms. :)

Pointers also allow us to be more effecient with data as we can pass pointers to large datatypes instead of the datatypes themselves.

Pointers to functions allow us to design event-driven callbacks.

Void pointers (void*) allow us to be generic.

Pointers pretty much allow us programmers to do everything worthwhile. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by luke2
In the early days, they used to copy all of the backbuffer's data to the front buffer and that, as you know, took up alot of memory and processing. Then some one came up with the Idea of using a pointer, instead of doing all that copying just change the pointer (which is only a few bytes to [at the time] huge kilo bytes) . I don't know when pointers were 'invented' but Id guess sometime around then.


You might find in fact they go back to the first stored program computer.

A pointer is a memory address. When it comes down to what the CPU is doing, whenever it need to fetch or store a value in memory, it has to use some sort of pointer. The address of a value in memory (RAM) is either an absolute value, a value stored in a register, or the sum of two or more of the above. Just like any other value you can load and store, you can perform arithmetic on memory addresses.

The C language was developed as an aid to develop the Unix operating system in a portable fashion in the late 1960s. Programming an operating system requires programming the CPU very close to the metal, including individual memory loads and stores. An abstraction of the manipulation of these memory addresses, or pointers, was an absolute necessity for an OS-development language, and C was no exception. The C++ language provides everything the C language provides, and then some.

So you see, pointers were invented many many years before anyone though of backbuffers. It really helps to just think of them as variables holding memory addresses of other variables.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Quote:
Original post by BrianMattJohnson
Pointers allow us to design good data-structures and algorithms. :)

Pointers also allow us to be more effecient with data as we can pass pointers to large datatypes instead of the datatypes themselves.

Pointers to functions allow us to design event-driven callbacks.

Void pointers (void*) allow us to be generic.

Pointers pretty much allow us programmers to do everything worthwhile. :)


There you go.

Share this post


Link to post
Share on other sites

Well, for example, with pointers you can allocate an array with a size you don't know before hands. That is impossible with static sized array.

Or perhaps, you need a linked list for keeping track of game objects, because you don't know how many objects there will ever be. Linked list is based on pointers, every member of the linked list has a pointer to the next object in the list (and usually to the previous elements). This is something you cannot accomplish without pointers.

Pointers alone, they only point to some part of memory. That's all and end of it. You never needed to "point" to anywhere?

Practically, an array with static size should be reconsidered if there is a possibility that you'll need a bigger array some day (and then you realize that eventually you'll end up increasing the size to infinity and thus are wasting memory).

I wouldn't say that pointers are about memory management. More like, they are needed to handle memory.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
@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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement