Pointers/References

Started by
4 comments, last by doyleman 17 years, 5 months ago
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?
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]
My Blog
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.
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.
My Blog
good work both of you, I now understand it, hopefully that helps the thread creator.
This indeed does help, thanks.

This topic is closed to new replies.

Advertisement