Jump to content
  • Advertisement
Sign in to follow this  
doyleman

Pointers/References

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

I'm having a super time </sarcasm> finding out the purpose of Pointers and References. I know how to declare them, return addresses or values from them, but I haven't a clue why they would be used in perhaps class methods, or functions, parameters. could anyone provide an example or help explain why i would want to use the heap instead of global?

Share this post


Link to post
Share on other sites
Advertisement
There are many reasons to use pointers, I'll just name two.
There is heap space and the stack and the second one is very limited because it is meant for procedures and variables that get called often and are small enough to fit into your CPU cache (this is not a requirement when programming though).
That is one of the reasons why heap space exists. Heap space can be quite big (depending on your OS, e.g. 32bit would be around 4GB) and usually resides in your RAM and if needed on HD.

Let's make and example, say you have a huge array:

double hugeArray[realBigNumber];

Let's say your compiler accepts this and actually makes it a stack variable, so that thing will sit in your stack while it is in scope and keep the space occupied.
In another case this might not even be possible because your stack isn't big enough.

Let's try the same thing on the heap:

double* hugeArrayPointer = new double[realBigNumber];

Now the hAP only hold the address of the first element of the array. As long as it's not bigger than 4GB you also can be sure that most systems will have no problems with that program.

If you want to pass that big array in your programm to some function all you need to do is pass the pointer:

someFunction(double* myPointer) { ... }

And to call it:

someFunction(hugeArrayPointer);

This way you just pass a (32/64bit) pointer to the function istead of copying the whole array and pass it to the function.
The last points didn't have anything to do with stack and heap btw., you could just as easily pass the stack by pointer:

someFunction(hugeArray);

If your function modifies the attribute variable and returns it back, you should better just pass it by reference, this will pass a dereferenced pointer to the function.

Hope it helps.


Edit: Assuming you are programming in C++ you need to delete the heap data when you don't need it anymore like this:

delete [] hugeArrayPointer; (or without the brackets if it's not an array)

The stack manages itself automatically, variables only live as long as their are in scope.

[Edited by - kiome on November 17, 2006 10:12:40 AM]

Share this post


Link to post
Share on other sites
When to use pointers, when to use references.. As for why, dynamic memory allocation , persistant but not permanent storage, and referencing another variable or memory location are the primary uses; whether a reference or pointer is preferred depends specifically on the situation at hand. The link is a fairly common rule of thumb.

Quote:

hAP only hold the address of the first element of the array and it's length.

This is incorrect; the length of an array is never maintained by the array itself in C++. The pointer contains only the address, nothing more. The runtime manages the size of the associated storage block somewhere, but only so that it knows what to delete when the time comes. You cannot recover this information unless you maintain it yourself.

Quote:

This way you just pass a (32/64bit) pointer to the function istead of copying the whole array and pass it to the function.
The last points didn't have anything to do with stack and heap btw., you could just as easily pass the stack by pointer:

someFunction(&hugeArray);


This is also wrong; the name of an array is the address of its 0th element, and arrays decay to pointers; the address-of operator is not required here.

Share this post


Link to post
Share on other sites
Thanks, I shouldn't have posted that late ;).

I wanted to use some random big data type first and then replaced it by an array, didn't think of the differences in this case though.

I better edit my post, or people might learn it the wrong way.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!