Sign in to follow this  

Pointers

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

Wow, can anyone shed some light on these? I understand the rest of the C++ language, but I can't wrap my head around these... Any advice and/or links to good resources would be awesome! Thanks guys! MMcNeil

Share this post


Link to post
Share on other sites
Yessir, I've looked in there... I still can't seem to understand them. I've got several amounts of tutorial information I'm sifting through, but it all seems to wanna teach it to me like I already know it... Lol.

MMcNeil

Share this post


Link to post
Share on other sites
A pointer variable is like a blank business card upon which you can write someone's address. You write the home address of the person you want to talk to onto the paper, and then later you can use that card to find that person. You can erase the address from that card and write in her work address instead. Or you can write in someone else's address.

Once you find that person, you can do whatever you normally could do with them.

The business card also says what that person is, like an accountant, and so when you find that person, that's what you're going to treat them as. You have to copy their address into a different business card if you want to treat them as something else.

Does that help? Pointers are something that a lot of people have a problem with. I'm not one of them, I understood pointers as soon as I read about them, and as such I don't have common ground to provide as much help as I would like.

Share this post


Link to post
Share on other sites
Quote:
Original post by Erzengeldeslichtes
A pointer variable is like a blank business card upon which you can write someone's address. You write the home address of the person you want to talk to onto the paper, and then later you can use that card to find that person. You can erase the address from that card and write in her work address instead. Or you can write in someone else's address.


A pointer variable can also be used to randomly select a person from a phonebook. It usually happens when you forget to initialize a pointer. If you are unlucky, a pointer can even select a person that's outside of the phone book (oh no!) in which case you have to jump out and deploy a parachute.

Share this post


Link to post
Share on other sites
There are two main categories of memory that the user can utilise to store program data. These are called the stack and the heap.

Stack

This is where space is allocated at compile time for the variables that you create - it's a named memory location that stores a value, for instance:

int aInt;

says that when this program is executed a named memory location of size (int) bytes needs to be allocated to the stack and named "aInt". The stack space assigned to aInt is released on program termination.

Heap

This is where your runtime memory allocations are assigned to when you declare and allocate heap space.

int *aInt;

Note that this time you are not naming a memory location were a value is stored, but instead naming a pointer object that is specifically pointing to a location on the heap.

When you assign space to a pointer, this data is pushed onto the heap and on deleting it is popped off.

Hope this clarifies. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by MMcNeil
Erz, you're my hero! That makes a ton of sense! Now to apply that to my programming :)

THanks a ton!

MMcneil


Glad to be someone's hero(ine). Maybe I should go into teaching, I seem to have a gift in explaining these things in ways that people actually understand.

I like deathkrush's extension to my metaphor. Of course, when you select someone at random and write their address into your business card, you're going to be asking a plumber to be an accountant, or an accountant to do brain surgery. You might even write an address to a vacant lot. They won't be happy if you do that, and they'll either crash you into a telephone pole or try to do the job and give very strange results.

You have to be careful that your business card is either blank or has the address to the right person (and you better check to make sure it's not blank before you go and try to find them, otherwise you'll crash into a tree. [wink]).

Of course, what's technically happening when this happens is important, particularly when you're debugging, but for now you can just understand what's going on metaphorically, and when you're comfortable enough with pointers you can then look into what's actually happening.

Stack and heap only serve to confuse the issue of pointers right now. It's important to know about the stack and the heap, but that's something to be dealt with later, when one is comfortable with pointers.

Share this post


Link to post
Share on other sites

This topic is 4167 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this