Sign in to follow this  
PureSnowX

Pointer to Pointer blackout question

Recommended Posts

This is a really dumb question and for some reason I'm totally blanking on it right about now. Inside my main-loop I'd like to have a pointer-to-pointer that points to an Screen-object pointer ( So if the screen pointer is assigned a new object to point towards the pointer-to-pointer is updated along.

However, I'm getting an error when trying to assign my pointer-to-pointer the screen pointer:
 

Screen* ScreenManager::activeScreen()
{
  return m_screen;
}

Screen** s = &screenManager.activeScreen();

 

I'm getting the following error:

 

 

Error: expression must be an lvalue or function designator


However this works just fine:
 

Screen* p = screenManager.activeScreen();
Screen** s = &p;

 

 

I'm sure this is VERY simple, but I just keep blanking where the problem lies.

Share this post


Link to post
Share on other sites

I think you want:

Screen** ScreenManager::activeScreen()
{
  return &m_screen;
}

Is this the way to do it? Because I'd rather avoid returning a pointer-to-pointer seeing as other functions just use a normal pointer.

Share this post


Link to post
Share on other sites
Screen** s = &screenManager.activeScreen();


Error: expression must be an lvalue or function designator

The reason for this error is that the pointer returned by activeScreen is a temporary variable. It will be destroyed after this line (or maybe somewhere during the execution of this line, I'm not really sure about this). If you tried to take the address of this temporary variable, the resulting pointer will point to a non-existent variable (the temporary). This would (if you're lucky) lead to a crash of your program, or worse, nondeterministic behaviour. That's why taking the address of temporaries (or rvalues) isn't allowed.

That's also the reason why your other example works: You save the returned pointer to an lvalue (which you call p), of which it is perfectly valid to take to address. But be carefull, if you do this within a function and p is local to the function, if you return &p or store it somewhere, you will get all the problems that taking the address of an rvalue would have caused. So be carefull. The correct solution is what SiCrane proposes. Or as Bob said, are you sure you need a pointer to a pointer? Edited by rnlf

Share this post


Link to post
Share on other sites
Well you could also return a reference to the pointer. Then other callers that don't need the address of the member won't need different syntax.
Screen *& ScreenManager::activeScreen() {
  return m_screen;
}
Either way you need a pointer or reference to the actual pointer you want to track changes to.

Share this post


Link to post
Share on other sites
This sounds dodgy to me.

Everywhere you feel the need to store the pointer-to-pointer, why not instead store a pointer to ScreenManager? Then when you need access to the screen, you call the activeScreen() method on that pointer?

Having a method that provides a pointer to internal data of a class (i.e. the pointer to screen stored in ScreenManager) means you lose track of where that member is modified. Not a good thing. Edited by Aardvajk

Share this post


Link to post
Share on other sites
Thanks all your suggestions are appreciated!

However, even if their is another way I'd like to learn WHY my code don't work and HOW to make it work like this ( if possible ) , returning a pointer and assigning it to a pointer-to-pointer in a single call.

If it's bad code or "fishy" is not of relevancy right now ( I'v gotten a lot of good input right now )


EDIT:
If I sound ungrateful that is not the case, it's just that I want to know why it doesn't work and how to make it work, even if it's bad code. Edited by Moonkis

Share this post


Link to post
Share on other sites
When you return a pointer by value, you get a copy of the pointer. There is just no way to use a copy of an object to get the address of the original object. A copy is a brand new object with no address information of the original object. You need to either return the address of the object in the first place (the Screen ** approach) or a reference to the original object (the Screen *& approach).

Share this post


Link to post
Share on other sites
When you return a pointer by value, you get a copy of the pointer. There is just no way to use a copy of an object to get the address of the original object. A copy is a brand new object with no address information of the original object. You need to either return the address of the object in the first place (the Screen ** approach) or a reference to the original object (the Screen *& approach).

Copy of the object? I'm guessing the object you are referring to is the pointer copy ( pointers are objects as well I guess ). 

But then why does assigning it to a normal pointer THEN assigning that pointer to a pointer-to-pointer work?
Is it because the pointer copy still refers to the same value address so the "original" pointer gets the pointer-copys values adress then we the pointer-to-pointer can get the "original" pointers adress?

Is it something like that? 

Share this post


Link to post
Share on other sites
But then why does assigning it to a normal pointer THEN assigning that pointer to a pointer-to-pointer work?
It doesn't. It compiles, but compiling is different from doing what you expect. It gets you the address of the copied pointer, not a pointer to the original pointer.

Share this post


Link to post
Share on other sites
But then why does assigning it to a normal pointer THEN assigning that pointer to a pointer-to-pointer work?
It doesn't. It compiles, but compiling is different from doing what you expect. It gets you the address of the copied pointer, not a pointer to the original pointer.

Because everything is return/pass by value? Is that why we only receive a copy of a pointer/copy of a reference object?

Share this post


Link to post
Share on other sites

[quote name='Moonkis' timestamp='1356814024' post='5015527']
Because everything is return/pass by value? Is that why we only receive a copy of a pointer/copy of a reference object?
[/quote]

 

At the heart of it, everything is always "pass by value". When you pass in or return a reference or pointer, the value of this reference or pointer (an address) are passed by value and therefor copied.

Share this post


Link to post
Share on other sites

Because everything is return/pass by value? Is that why we only receive a copy of a pointer/copy of a reference object?

 

 

At the heart of it, everything is always "pass by value". When you pass in or return a reference or pointer, the value of this reference or pointer (an address) are passed by value and therefor copied.

Yeah that is what I meant, though it's easy to forget that when dealing with pointers and references ( since I did ) in the beginning. Because they "act" like a magical solution that makes you modify the values from anywhere instead of just locally.

 

Nearly all books I'v read has failed to mention that EVERYTHING is at heart pass by value, especially when introducing pointers.

Share this post


Link to post
Share on other sites

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