Jump to content
  • Advertisement
Sign in to follow this  
HeyHoHey

c++ memory/pointers?

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

im new to programming in general and i just read the section on pointers. so &X is the location of x right? and int *p; defines a pointer then you set them equal so the pointer points to X's memory location. but what is the point of this? what does knowing a variables memory location mean? what practical things do you do with this info? thanks hey

Share this post


Link to post
Share on other sites
Advertisement
I tend to think of &foo as "The-address-of foo", *foo as "The-data-at foo", and int* foo as "int pointer called foo".

The main use for pointers is dynamic memory allocation. Since you don't know what else is in memory, you ask for a chunk of memory, and the runtime library will give you a pointer, which is essentially the address of the start of that memory. That lets you allocate as much memory as you need, without having to always assume the worst case.

Another use for pointers is so a function can change a variable without having to return a copy. For instance, the Win32 GetCursorPos() function:

POINT pt;
GetCursorPos(&pt); // Pass the address of pt to GetCursorPos


That's slightly more efficient that returning the point as a return value, and also frees up the return value for something else (E.g. success / failure code).

There's plenty of other examples, but that's the basics. I'm sure someone will point out a good tutorial on the subject.

Share this post


Link to post
Share on other sites
So the book doesn't explain how they can be used? or a search proved unfruitful?
or maybe you just could not be bothered?

http://www.gamedev.net/reference/articles/article1697.asp
http://www.gamedev.net/reference/articles/article1869.asp

Yes it does annoy me when people don't try and find the answer themselves, instead using the "quick option" of asking someone else.

Share this post


Link to post
Share on other sites
You will also find them to be immensely useful when you learn about data structures (an important subject that any programmer should spend some time on). Pointers are one of the most primitive forms of the general concept of references, without which linked lists, binary trees, and so forth would be impossible to make. (At a very minimum, you'd need indexes, which are essentially what pointers are anyway.)

Share this post


Link to post
Share on other sites
I thought the same thing about a few days ago until it all clicked while doing an assignment for school. Once you get the idea of allocating memory via new, then your like cool, I can point to a nameless variable!!

Just don't forget to delte the dynamic var/array. Just keep at it, it will come to you!!!!!

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!