Skip to main content
gamedev.net
🔒 Locked

Pointers

Started by GamerDude27 Dec 23, 2006 at 4:58 PM 5 replies 700+ views
Original Post
GamerDude27
GamerDude27
I just read the tutorial on pointers from cplusplus.com and I understand it fairly well. I was wondering how much you use this in programming and an example on why to use it. Thanks in advance! GamerDude
Driv3MeFar
Driv3MeFar
Pointers are both incredibly usefull and incredibly troublesome in any non-trivial piece of C++ code, games included. It would be impossible to use or make a linked list data structure, for example, without pointers.
dbzprogrammer
dbzprogrammer
Pointers are critical to programming. In assembler, pointers are used to fetch data from memory, and without that, you're program would have little to no functionality.

There's a 1 MB limit on the stack, or the way you normally allocate data. When you need to go over that 1 MB, you need the free store. To access the free store, you need pointers.

Pointers are a hard concept, they don't make much sense, and the best way to understand there use it practice using them.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Cherez
Cherez
Pointers are necessary for any recursive structure, such as:

struct foo
{
char a;
foo b;
}

That would mean the size of foo is sizeof(char) + sizeof(foo). That is why linked lists require pointers, as pointers are always a set size (usually 4 bytes on modern architectures.) They also let you not allocate structures until they're needed. This saves a lot of RAM and cycles that would otherwise be spent allocating memory that isn't ever going to be used.

Quote:
Original post by dbzprogrammer
There's a 1 MB limit on the stack, or the way you normally allocate data. When you need to go over that 1 MB, you need the free store. To access the free store, you need pointers.


That's entirely platform dependent. On Linux, for instance, the stack is expanded as needed. On Windows I believe its size is included in the PE header.
dbzprogrammer
dbzprogrammer
It's an example...

No offense to you Cherez, but, that's a insignficant point in this thread. The OP is trying to understand the revelance of pointers, not the difference between Linux and Windows heap sizes. I just get a little tired of people doing that, nothing against you personally. I limit myself to 500kb on the stack.

Anyways, pointers have multiple uses. As the other posters have said, a linked list needs to know where the next element in the list is. They do this by a having a pointer reference to the next element. You can also allocate a chunk of data on the free store and keep its location in memory inside a pointer. You can globally access a chunk of data, it doesn't get popped off the stack. You can structure data to be sent across a network. You can use C-strings. You can actually store data in a structural way that you haven't defined (although this is quite advanced and "dangerous"). I'm still thinking of things that you can use them for.

There functionality is almost boundless. If you want a really good example, take a look at BrainF*ck.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Zakwayda
Zakwayda
Just to balance things out, it should probably be mentioned that in C++, use of raw pointers can be avoided for the most part. The use of references, standard library containers and algorithms, auto_ptr, std::string, Boost's various smart pointer classes, and any of the many other useful tools available to the C++ programmer can all but eliminate the direct use of pointers in one's code.

In fact, it's entirely possible to write a complete C++ program without using a single raw pointer.
Zahlman
Zahlman
Quote:
Original post by GamerDude27
and I understand it fairly well.


Quote:
Original post by GamerDude27
...I was wondering ... why to use it.


Umm... are you sure?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.