Jump to content

  • Log In with Google      Sign In   
  • Create Account

a = b = new c?


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
39 replies to this topic

#1 Waaayoff   Members   -  Reputation: 783

Like
0Likes
Like

Posted 28 June 2013 - 04:29 AM

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


"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "

Sponsor:

#2 BitMaster   Crossbones+   -  Reputation: 4427

Like
7Likes
Like

Posted 28 June 2013 - 04:41 AM

There is only one new, so obviously a c is dynamically allocated once and the address stored in both a and b.


Edited by BitMaster, 02 July 2013 - 02:36 AM.


#3 samoth   Crossbones+   -  Reputation: 5032

Like
9Likes
Like

Posted 28 June 2013 - 04:43 AM

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.



#4 Waaayoff   Members   -  Reputation: 783

Like
0Likes
Like

Posted 28 June 2013 - 04:46 AM

Ok thanks :)


"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "

#5 NightCreature83   Crossbones+   -  Reputation: 3032

Like
2Likes
Like

Posted 28 June 2013 - 05:44 AM

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();

 


Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#6 Bregma   Crossbones+   -  Reputation: 5427

Like
2Likes
Like

Posted 28 June 2013 - 08:10 AM

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()));

Stephen M. Webb
Professional Free Software Developer

#7 frob   Moderators   -  Reputation: 22710

Like
1Likes
Like

Posted 28 June 2013 - 09:41 AM

It is also the same as:

 

b = new c;

a = b;


Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.


#8 Vortez   Crossbones+   -  Reputation: 2704

Like
0Likes
Like

Posted 28 June 2013 - 10:30 AM

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



#9 frob   Moderators   -  Reputation: 22710

Like
1Likes
Like

Posted 28 June 2013 - 11:23 AM

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. 


Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.


#10 Waaayoff   Members   -  Reputation: 783

Like
0Likes
Like

Posted 28 June 2013 - 11:36 AM

 

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


"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "

#11 Camilo   Members   -  Reputation: 189

Like
2Likes
Like

Posted 29 June 2013 - 01:17 AM

[...]

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.



#12 Tom KQT   Members   -  Reputation: 1621

Like
0Likes
Like

Posted 29 June 2013 - 02:36 AM

 

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, 29 June 2013 - 02:38 AM.


#13 Waaayoff   Members   -  Reputation: 783

Like
0Likes
Like

Posted 29 June 2013 - 01:15 PM

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, 29 June 2013 - 01:17 PM.

"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "

#14 Brother Bob   Moderators   -  Reputation: 8576

Like
2Likes
Like

Posted 29 June 2013 - 01:41 PM

 

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, 29 June 2013 - 01:43 PM.


#15 Waaayoff   Members   -  Reputation: 783

Like
0Likes
Like

Posted 30 June 2013 - 02:30 AM

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


"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "

#16 Tom KQT   Members   -  Reputation: 1621

Like
1Likes
Like

Posted 30 June 2013 - 02:56 AM

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



#17 Waaayoff   Members   -  Reputation: 783

Like
2Likes
Like

Posted 30 June 2013 - 04:09 AM

 

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)


"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "

#18 m3mb3rsh1p   Members   -  Reputation: 131

Like
0Likes
Like

Posted 30 June 2013 - 12:17 PM

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, 30 June 2013 - 12:17 PM.


#19 Serapth   Crossbones+   -  Reputation: 5743

Like
0Likes
Like

Posted 30 June 2013 - 12:22 PM

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, 30 June 2013 - 04:52 PM.


#20 Khatharr   Crossbones+   -  Reputation: 3038

Like
0Likes
Like

Posted 30 June 2013 - 01:45 PM

 

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


void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.




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