• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
PureSnowX

Pointer to Pointer blackout question

13 posts in this topic

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.

0

Share this post


Link to post
Share on other sites
I think you want:
Screen** ScreenManager::activeScreen()
{
  return &m_screen;
}
1

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.

0

Share this post


Link to post
Share on other sites

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

[code]

Screen* s = screenManager.activeScreen();
[/code]
2

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
0

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

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
0

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
0

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

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? 

0

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

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?

0

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.

0

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.

0

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  
Followers 0