C++: Why to use pointers?

Started by
28 comments, last by BeanDog 18 years, 3 months ago
How about this problem (I don't know if someone said it already, i didn't read all the replies):

if you create a function that gives an int back, you can use it like this:
int myint = myfunction(param1, param2, ...);

But what if you want to make a function that need to returns 2 or more value's, you can use pointers like this:
myfunction(&myvar1, &myvar2, &myvar3, ..., myparam1, myparam2, ...);

that way you can also access myvar1, ... an change those values.
Spippo is magic,is magic!!!!!Oh ho hoo!!!!Spippo is magic
Advertisement
Quote:Original post by Spippo
But what if you want to make a function that need to returns 2 or more value's, you can use pointers like this:
myfunction(&myvar1, &myvar2, &myvar3, ..., myparam1, myparam2, ...);


Unless you want your code to be interfaced by C code, you should use references. As I mentioned in my example above, your code forces &myvar1 to be stored in a memory area where it can be addressed, even if it is an inner-loop variable that should have been put in a register instead.

Using references, the compiler is free to decide if he should implement this operation using memory addressing, or by inlining myfunction and having it access the register where the variable is stored.

Yes. Using a reference provides more information to the compiler all-around, while also simplifying your work (by letting you not worry about pointer syntax). As a rule, use references where you can, and pointers where you have to. This is basically for the same reason we prefer more specific control structures like if, while, for etc. where possible, and use goto as a last resort (if at all).

As for memory management stuff... use the standard library containers. Please.
Quote:Original post by Iftah
Quote:Original post by mikeman
If your code works fine without pointers, then you don't need them. One out of the many examples where pointers(either raw C++ pointers or STL constructs like smart pointers) have a use is when you want to store a reference to an object. Say you are making an FPS, and you want to be able to "lock" a specific target and kill him when you shoot:

*** Source Snippet Removed ***


thats not a very robust example... what if two players lock on same target?
one player will shoot and kill, the second player will shoot and make the application collapse.

and cant the same be done with references?


I just wrote a very short example just to demonstrate the use of pointers, I didn't say you can actually implement a multiplayer FPS with that for god's sakes.

And references are not resetable, you initialize like "int &a=b" and that's it. Variable "a" refers to "b" for the rest of its life.
Pointers are not just used for store equivalent data types; another good use of pointer is for storing the direction of a dinamicaly allocated block in memory where you put raw data, like loading a picture, sound, compressed data, etc.


good luck.
i dont quite understand what they all go on about, only consider im loading a map into an array. (an array true is a pointer infact) Now lets say i dont know the map size, surely there is no workaround for this

float *map;

// Read TXT FIle
map = new float[sizex][sizey];

// Need to load next map
delete [] map;

..Back to the top


Or consider where lots of units need to infact "point" to the other unit. Arrays are out of the question because in some cases alot of units will exist and in some cases only 1.

unit
{
vector position
unit *target;
unit *commander; // For a hierarchyal play
unit *previousTarget;
unit *largestThreat;
}

If you have 100 units then you could use a pointer and a number, but if you have 1000000 units and then in the next game only 10 then arrays are out of the option and you must use lists
----------------------------

http://djoubert.co.uk
Quote:Original post by dawidjoubert
i dont quite understand what they all go on about, only consider im loading a map into an array. (an array true is a pointer infact) Now lets say i dont know the map size, surely there is no workaround for this

Or consider where lots of units need to infact "point" to the other unit. Arrays are out of the question because in some cases alot of units will exist and in some cases only 1.

If you have 100 units then you could use a pointer and a number, but if you have 1000000 units and then in the next game only 10 then arrays are out of the option and you must use lists


It seems to me that in all these cases you could have used a dynamic container, such as vector or deque. And, of course, smart pointers are a good idea as well, but they're still pointers.
Quote:Original post by BeatOne
No seriously, I still don't understand the major advantage using pointers, other than memoryefficient..?


You'll find out. Until then, don't use them.

It's not like pointers are something you must embrace and love to use everywhere.
They're just a tool you occasionally need to solve the problems you're faced with.

So as long as you know how they work and what they do, don't bother about them if you can't see a reason to use them.

You'll figure it out when you run into something where you need them.
P.S. (to my previous post, what was it now... the forth?):

What about virtual methods? How would I accomplish this (my code):
vector<entity*> objects;objects.push_back(new ball());objects.push_back(new box());objects.push_back(new enemy());

Obviosly you can't do with with references nor the stack.

Good luck!

C++
Quote:Original post by Guimo
a. Create a class about 100 bytes long (very common)
b. Create an array about 10000 elements of that array (very common)
c. Pass the array to a function... will you risk passing 1Mb each call with the appropiate stack allocation or just 4 bytes for a pointer?


Maybe just a technicality, but a correction is appropriate for an audience which is still determining the use of pointers:

Arrays are passed by pointer in C++. That is, the function
void foo(int[] array);

is equivalent to
void foo(int* array);


It can be harmful to assume that your array is being passed by value. When you declare an array:
int var[10];

var is actually a pointer to the first element of the array, and it is that pointer that you pass to a function.



~BenDilts( void );

This topic is closed to new replies.

Advertisement