Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Pointer to Pointer blackout question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 Moonkis   Members   -  Reputation: 273

Like
0Likes
Like

Posted 22 December 2012 - 03:35 PM

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.



Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9662

Like
1Likes
Like

Posted 22 December 2012 - 03:36 PM

I think you want:
Screen** ScreenManager::activeScreen()
{
  return &m_screen;
}


#3 Moonkis   Members   -  Reputation: 273

Like
0Likes
Like

Posted 22 December 2012 - 03:44 PM

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.



#4 Brother Bob   Moderators   -  Reputation: 8566

Like
2Likes
Like

Posted 22 December 2012 - 03:46 PM

What do you want to accomplish with a double pointer that you cannot do with a single pointer?

</p><pre>
Screen* s = screenManager.activeScreen();</pre>


#5 rnlf   Members   -  Reputation: 1185

Like
0Likes
Like

Posted 22 December 2012 - 04:42 PM

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, 22 December 2012 - 04:43 PM.

my blog (German)


#6 SiCrane   Moderators   -  Reputation: 9662

Like
0Likes
Like

Posted 22 December 2012 - 04:42 PM

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.

#7 Aardvajk   Crossbones+   -  Reputation: 6179

Like
0Likes
Like

Posted 23 December 2012 - 05:45 AM

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, 23 December 2012 - 05:46 AM.


#8 Moonkis   Members   -  Reputation: 273

Like
0Likes
Like

Posted 29 December 2012 - 10:53 AM

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, 29 December 2012 - 10:57 AM.


#9 SiCrane   Moderators   -  Reputation: 9662

Like
0Likes
Like

Posted 29 December 2012 - 11:00 AM

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).

#10 Moonkis   Members   -  Reputation: 273

Like
0Likes
Like

Posted 29 December 2012 - 11:08 AM

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? 



#11 SiCrane   Moderators   -  Reputation: 9662

Like
0Likes
Like

Posted 29 December 2012 - 11:12 AM

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.

#12 Moonkis   Members   -  Reputation: 273

Like
0Likes
Like

Posted 29 December 2012 - 02:47 PM

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?



#13 Rattenhirn   Crossbones+   -  Reputation: 1806

Like
0Likes
Like

Posted 29 December 2012 - 03:37 PM

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.



#14 Moonkis   Members   -  Reputation: 273

Like
0Likes
Like

Posted 29 December 2012 - 05:51 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS