a = b = new c?

Started by
36 comments, last by Paradigm Shifter 10 years, 9 months ago

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. "
Advertisement

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

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.

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

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, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

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

It is also the same as:

b = new c;

a = b;

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

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.

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

This topic is closed to new replies.

Advertisement