Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problems handing a pointer as a variable

Started by Rasmadrak Sep 25, 2007 at 2:26 AM 4 replies 1.2k views
Original Post
Rasmadrak
Rasmadrak
Hi, I'm curious, why doesnt' the following function work? or rather, why doesn't the allocated memory stay beyond the scope of the function that created it?



void doStuff(float *Kalle)
{
   Kalle = new float[100];
   //Kalle == float[100]...
}

//...

main loop:
{
   doStuff(Kalle);
   //Kalle == 0x0.... wtf?
}


I can easily bypass this problem by creating float[100] and handing that to the doStuff function, but that doesn't allow for a clean user-transparent code. So, anyone? /Robert
"Game Maker For Life, probably never professional thou." =)
ToohrVyk
ToohrVyk
C++ passes arguments by value, unless you specify otherwise. In your case, passing your pointer by reference (char* &Kalle) would work, though passing a reference to a vector would solve the ownership ambiguities as well.
instinKt
instinKt
When you pass your pointer to the function, the pointer is _copied_. Once in the function you are no longer manipulating the original pointer but a copy of it. Thus, when you return, that copy is destroyed (your allocated memory will be lost causing a memory leak) and you're left with the original pointer.

To do what you're wanting, you need to pass a pointer to the pointer.

void doStuff(float **Kalle){   *Kalle = new float[100];   //Kalle == float[100]...}//...main loop:{   doStuff(&Kalle);   //Kalle == 0x0.... wtf?}


Notice the double ** (pointer to pointer), when new is called you're dereferencing the first pointer with the single *, and when you pass the pointer to the function you give it the address of the original pointer using &.

It can get a bit confusing, but the more you play around with it, the more you'll understand.
Daishim
Daishim
The allocated memory actually does exist beyond the doStuff function, however, a little pointer explanation is in order. What you are passing to doStuff is a memory address, which it copies to your pointer that becomes the argument. The argument variable is then assigned new memory, and then points to a new memory location, while your original variable is untouched. To correct this, you want to pass a pointer reference, like so:

void doStuff(float *&Kalle){     Kalle = new float[100];}int main(void){     float *My_Stuff = NULL;     doStuff(My_Stuff);     ...}

I know only that which I know, but I do not know what I know.
Rasmadrak
Rasmadrak
Hi!

Thanks for your replies, it all makes sense now - actually I'm rather embarresed that it was that simple. :)

Pointers - can't live with them, can't kill them!

Thanks again!


/Robert
"Game Maker For Life, probably never professional thou." =)
instinKt
instinKt
Quote:
Original post by Rasmadrak
Pointers - can't live with them, can't kill them!


Sure you can! It just hurts you more than them. :(

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.