Jump to content
  • Advertisement
Sign in to follow this  
Rasmadrak

Problems handing a pointer as a variable

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

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);

...
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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. :(

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!