• 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
Waaayoff

a = b = new c?

37 posts in this topic

If a and b are pointers, does this

 

A) Allocate memory once for b and also store address in a

B) Allocate memory twice, once for a and once for b

0

Share this post


Link to post
Share on other sites

You should never write code like that as this doesn't mean the same, and the way that statement is written will often degenerate into this:

int* a = b = new c();

 

2

Share this post


Link to post
Share on other sites

The assignment operator binds left, which means

a = b = new c;

is the same as

a = (b = new c);

or

a.operator=(b.operator=(c.operator new()));
2

Share this post


Link to post
Share on other sites

What would be the point of such code??? One pointer is enough imo...

 

Depends on the algorithm.  

 

There are many that require two pointers, one that will always be there and another that gets modified.  In this case they are initializing both pointers at once. 

1

Share this post


Link to post
Share on other sites

 

What would be the point of such code??? One pointer is enough imo...

 

Depends on the algorithm.  

 

There are many that require two pointers, one that will always be there and another that gets modified.  In this case they are initializing both pointers at once. 

 

 

Yep that's why i'm doing it

0

Share this post


Link to post
Share on other sites

[...]

Yep that's why i'm doing it

 

As a pedagogical exercise it's an interesting question, but don't use it. If you have to ask on a forum what it does the next person that sees your code (or yourself in a few months) might not know, either. Not worth it, just to save 1 line of code.

2

Share this post


Link to post
Share on other sites

 

As a pedagogical exercise it's an interesting question, but don't use it. If you have to ask on a forum what it does the next person that sees your code (or yourself in a few months) might not know, either. Not worth it, just to save 1 line of code.

Exactly. This will not make the code run faster. It even won't make the code more readable (quite the opposite), so why bother?

I personaly would definitely put the a = b part on a separate row and maybe also add a short comment why am I requesting another copy of the pointer (or use such a name that would suggest it clearly).

Edited by Tom KQT
0

Share this post


Link to post
Share on other sites

What about when you're doing something like this:

 

ShaderParameter* ParameterManager::CreateFloat(const std::string& Name)
{
      ShaderParameter* pParam = mParameters[Name];


     if (!pParam)
     {
          mParameters[Name] = pParam = new FloatParameter;
     }


     return pParam;
}
Edited by Waaayoff
0

Share this post


Link to post
Share on other sites

 

What about when you're doing something like this:

 

ShaderParameter* ParameterManager::CreateFloat(const std::string& Name)
{
      ShaderParameter* pParam = mParameters[Name];


     if (!pParam)
     {
          mParameters[Name] = pParam = new FloatParameter;
     }


     return pParam;
}

 

What are you gaining over just having two separate assignment expressions?

pParam = new FloatParameter;
mParameters[Name] = pParam;

Trying to be clever when it comes to coding is rarely a benefit over being clear. The difference is purely in the information you convey to the programmer; the compiler couldn't care less. Your code does not have to be clear because it benefits the compiler, it has to be clear because YOU, the programmer, has to read the code.

Edited by Brother Bob
2

Share this post


Link to post
Share on other sites

Well it's not an attempt at optimization i assure you. Also, I didn't realize it was that unclear to read...

0

Share this post


Link to post
Share on other sites

Also, I didn't realize it was that unclear to read...

Then why did you create this thread? ;-)

You weren't sure how it works, that's IMHO a good proof that it isn't clear to read (at least for you and as it's your code...).

1

Share this post


Link to post
Share on other sites

 

Also, I didn't realize it was that unclear to read...

Then why did you create this thread? ;-)

You weren't sure how it works, that's IMHO a good proof that it isn't clear to read (at least for you and as it's your code...).

 

 

Yes but that's like saying i shouldn't use smart pointers because they're not readable for me since i don't know how they work.. I get what you guys are saying about readability but i just thought this was a simple assignment operation that was common knowledge for non beginners (unlike me)

2

Share this post


Link to post
Share on other sites

There's also the type casting taking place:

T a = b = (T)new c();
mParameters[Name] = pParam = (ShaderParameter)new FloatParameter;

...or however it should be done.

 

I think it would be clearer to have the cast or temporary identified on a separate line.

Edited by m3mb3rsh1p
0

Share this post


Link to post
Share on other sites

a = b = new c creates (allocates) one object of type c, then assigns the pointer to b, and then assigns the same pointer to a.

Careful, that isn't actually true.

 

a will point to b, while b will point to c.  The fact that b currently points to c wont hold true if b is reassigned.

 

... I think I said that poorly.

 

 

EDIT: ------------------------------------------------------ IGNORE EVERYTHING I SAY IN THIS THREAD!!!! -----------------------------------------------------------------

EDIT: Seriously... do it.  Took a double dose of stupid pills this afternoon.  I'd edit out my comments for the sake of confusion, but thats makes things worse.

Edited by Serapth
0

Share this post


Link to post
Share on other sites

There's also the type casting taking place:

T a = b = (T)new c();
mParameters[Name] = pParam = (ShaderParameter)new FloatParameter;

...or however it should be done.

 

I think it would be clearer to have the cast or temporary identified on a separate line.

Er, no.  There is no casting.

 

Assuming a and b are of type pointer to c, then assigning the value of b to a requires no type cast, and the expression new c returns a value of type pointer to c, so again there is no type cast.

 

Again, assuming pParam is of type ShaderParameter* and mParameters is of type std::map<std::string, ShaderParameter*> and that FloatParameter is a class derived from ShaderParameter, there is no implicit type cast anywhere in the expression.  Of course, those assumptions may not hold true, in which case the code woul be unlikely to compile anyway because it's missing explicit casts.

 

So, I think adding explicit casts where no cast is required would confuse the reader.  I don't have a problem with the multiple expressions on the same line, but then I've had to maintain maybe a million lines of C code over the years and recognize the idiom.  There are many things that are far more important to readability than that (eg. max 80 character lines).

0

Share this post


Link to post
Share on other sites

 

 

a = b = new c creates (allocates) one object of type c, then assigns the pointer to b, and then assigns the same pointer to a.

Careful, that isn't actually true.
 
a will point to b, while b will point to c.  The fact that b currently points to c wont hold true if b is reassigned.
 
... I think I said that poorly.

 

http://objection.mrdictionary.net/go.php?n=6774974

 

 

Actually that captures it perfectly ( and is completely wrong! ;) ).

 

The address of b is assigned to a, not the value.  This is exactly what I was warning of.

0

Share this post


Link to post
Share on other sites

 

 

 

a = b = new c creates (allocates) one object of type c, then assigns the pointer to b, and then assigns the same pointer to a.

Careful, that isn't actually true.
 
a will point to b, while b will point to c.  The fact that b currently points to c wont hold true if b is reassigned.
 
... I think I said that poorly.

 

http://objection.mrdictionary.net/go.php?n=6774974

 

 

Actually that captures it perfectly ( and is completely wrong! ;) ).

 

The address of b is assigned to a, not the value.  This is exactly what I was warning of.

 

Why don't we just let a compiler answer it instead.

int main()
{
    int *a, *b;

    a = b = new int;

    std::cout << "value of a is " << a << std::endl;
    std::cout << "value of b is " << b << std::endl;
    std::cout << "address of b is " << &b << std::endl;
}

Is the value of a equal to the value of b or to the address of b?

0

Share this post


Link to post
Share on other sites

 

 

 

 

a = b = new c creates (allocates) one object of type c, then assigns the pointer to b, and then assigns the same pointer to a.

Careful, that isn't actually true.
 
a will point to b, while b will point to c.  The fact that b currently points to c wont hold true if b is reassigned.
 
... I think I said that poorly.

 

http://objection.mrdictionary.net/go.php?n=6774974

 

 

Actually that captures it perfectly ( and is completely wrong! ;) ).

 

The address of b is assigned to a, not the value.  This is exactly what I was warning of.

 

Why don't we just let a compiler answer it instead.

int main()
{
    int *a, *b;

    a = b = new int;

    std::cout << "value of a is " << a << std::endl;
    std::cout << "value of b is " << b << std::endl;
    std::cout << "address of b is " << &b << std::endl;
}

Is the value of a equal to the value of b or to the address of b?

 

 

The value of a and b are the same ( for now ), the address of a and b are different.

 

The address pointed to by a and b will be the same (for now), the address OF a and b will be different.

Edited by Serapth
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