Jump to content
  • Advertisement
Sign in to follow this  
Jettoz

Pointers, when are they used?

This topic is 4134 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

Hello everyone, I'm doing some reading on pointers and did a small test to see what a pointer can do so far. (I'm still new to learning Pointers)
#include <iostream>

using namespace std;

int main ()
{
	// Vars
	int Number, *NumberPtr;

	// Set Vars
	Number = 100;
	NumberPtr = &Number;

	// Print Number & NumberPtr
	cout << Number; // Displays Number Value
	cout << endl;
	cout << NumberPtr; // Number Address in Memory (HEX)
	cout << endl;
	cout << &Number; // Number Address in Memory (HEX)
	cout << endl;
	cout << *NumberPtr; // Value of NumberPtr

	cin.get();

	return 0;
}



Now all I understand is I made a variable Number with the value 100 and a Pointer NumberPtr which store the address of Number (Hex) and it's value. So I can print out Number which is 100 and than print out it's Memory from NumberPtr which is 0012FF60 and than I printed &Number which also gives it's Memory address just like the pointer did (Which it should) 0012FF60. Than I printed out *NumberPtr which gives that value of Number. Now my question here is what is the real point of doing this? When are pointers really used for example if I made a Console Text Game where would pointers come in use? Or if I made a 2D Pac Man game using an API why would pointers be used? I understand about Hex codes however because I use to do some 'hacking' on single player games before but I'm still confused on why a pointer is used even though I some what understand the basics of what it does. Thanks for any help you can provide!

Share this post


Link to post
Share on other sites
Advertisement
-Pointers are smaller then objects (They are useually the size of an integer),
so they are much more effeciant when copying

Deep copies accure when an entire object is copied to another.
Shallow copy only copies its 4byte address--much more effeciant.

-Shared objects. Pointers are the only way of sharing an object

-Dynamic memory. You have to use pointers when allocating/deallocating memory

There are many more reasons then this.

It is much easier to pass a 4byte address then a larger object on the
stack when passing parameters.

When programming low level, you will find out pointers are used
virtually everywhere.

Share this post


Link to post
Share on other sites
I need to really document this somewhere...


Generally, pointers are to data what links are to websites.

A link isn't a website, but it tells you where it is. It's far smaller than a website. The website itself is only one instance. If you copy the website, then if it changes the copy doesn't get those changes. A link though always points to the 'current' website. Of course if the website goes down, the link is broken and badness occurs.


Thus, a pointer behaves like a web link. It's not data, but tells you where it is. You actually have to follow (dereference) it to get the data. If it's wrong (a null pointer, typo'd link) stuff will break. If the data is gone (deleted data, webserver goes down) stuff will break.


So, think to yourself when you would pass around web links rather than copying an entire website. Think when you'd place links in a webpage rather than copying a(n) image/movie/text.

[edit: and this isn't so important now, but there are better ways than pointers to do what pointers do in 98% of modern C++]

Share this post


Link to post
Share on other sites
the real use of pointer is dynamic memory allocation
instead of setting a pointer to &another_variable you can create a new area of memory with the new keyword, you can create primitive types like ints, objects or arrays of either and set the size at runtime

also sometime its usefully to give two different areas of code access to a single variable or object

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
I need to really document this somewhere...

that or someone needs to make a gdnet article on "practical uses for pointers" for linking to

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
snip


That's a great description! ++

OP-

The misuse of pointers is one of the main causes (if not the main cause) of program crashes. Not trying to scare you, but it's something you should be aware of. I suggest you learn the difference between pass-by-pointer and pass-by-reference early on because it'll save you some debugging headaches later. Basically unless your data is dynamic memory, pass-by-reference is the safer way to go, and has the benefit of being a lot more comfortable to use too.

Share this post


Link to post
Share on other sites
Alright thanks everyone, I'll keep reading more about Pointers and check online for some examples of Pointers being used. Unless I'm wrong it wouldn't be that big of problem to make a Text RPG without using pointers just for learning for now. I've done one Text RPG so far and didn't use any Pointers so I'm going to make another one and use Pointers to help me learn them later down the road.

Share this post


Link to post
Share on other sites
Quote:

I've done one Text RPG so far and didn't use any Pointers

A string, as being an array of characters, is essentially a pointer:

char str[]="Hello!";

// *str == 'H'; str points to first char in string

...Just another place pointers are used[smile]

As being a text RPG Im sure theres pointers..

Share this post


Link to post
Share on other sites
I am working on a program right now which will find pointers invaluable. Let me illustrate.

In my world I am going to have all sorts of 3D objects - crates, chairs lamps etc. Now, you see, each object will have to have a 3D mesh, textures, normal maps, light maps etc. that define what the object looks like. Now considering that I may have multiple instances of a single object (i.e. 50 crates on one map), each object will have to have the same mesh and textures. So imagine copying fifty times - that's a LOT of memeory usage!
But instead of each object having its own mesh, it will merely have a POINTER to a mesh. This way, each of the 50 crates would point to the exact same mesh instead of creating copies of it.

And thus, pointers have cut my memory usage 50 times (not exactly as pointers themselves take memory, but that memory is neglibile compared to how much memory the meshes take themselves).

Share this post


Link to post
Share on other sites
Wow thanks, now I understand the reason for Pointers! Right now because I'm still using Console for my text games pointers are not a big issue for memory until I get into Graphics. Thanks again everyone!

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!