Sign in to follow this  

Pointer Swapping?

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

Let's say I have the following hypothetical program:
int *get_pointer_a()
{
  return a;  // some 'int *a' only availible through this function
}

int *get_pointer_b()
{
  return b;  // some 'int *b' only availible through this function
}

void swap()
{
  //   use get_pointer_a() and get_pointer_b() to somehow
  // swap what 'a' and 'b' point to.
}


How might I be able to swap what 'a' is pointing to with what 'b' is pointing to when all I have availible is those two functions? This may sound strange, but this is somewhat similar to the problem I encountered while working on a game.

Share this post


Link to post
Share on other sites
You can't. Not with what you have. Your functions return a copy of your pointers a and b respectively. Changing those will only affect the COPIES of those pointers. The original pointers will still point to the same thing.

You CAN modify the objects your pointers refer to, of course.

Share this post


Link to post
Share on other sites

void swap()
{
int* a = get_pointer_a();
int* b = get_pointer_b();
int temp = *a;
*a = *b;
*b = temp;
}


That will change the values that they point to.

Share this post


Link to post
Share on other sites
The only way you could change the value of A and B is if they returned references, or pointers-to-pointers.

Also, how would "a" only be accessible to the first function? If it's a local, then the pointer won't even be valid when outside the scope of the first function.

Share this post


Link to post
Share on other sites
If I understand you properly, what you want to do is NOT:

void swap()
{
int *a = get_pointer_a();
int *b = get_pointer_b();

std::swap(a,b);
}


but rather some operation that would change the subsequent return values of the functions (e.g., if they previous returned addresses 1000 and 2000 respectively, they'd subsequently return 2000 and 1000, respectively).

In which case, with just those two functions and no other access to the pointers that are being returned, you cannot. And as others have mentioned, if the returned pointers are locally (stack) allocated and not made unavailable by some other means (such as access rights), the entire system is invalid.

Share this post


Link to post
Share on other sites
I think I need to get a little more pratice with object oriented design, because weird problems like these always seem to pop up and give me trouble... I'm trying to switch from a hacker/procedural programmer to an object oriented programmer.

The main reason why I was having such a hard time figuring it out was because I thought it would be impossible. Now, that I have this confirmed ... I'm not sure what I'm going to attempt doing...

Share this post


Link to post
Share on other sites

This topic is 4302 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this