order of execution

W

Wolfgang Jeltsch

Hello,

am I right that in
p = new SomeClass(p)
the newly constructed object is initialized and therefore the p in the
brackets is used before the assignment of the new pointer to p takes place?

Is it generally guaranteed that all side effects of an expression have taken
place before the expression's result is used as an operator argument or
function parameter?

Wolfgang
 
R

Ron Natalie

Wolfgang Jeltsch said:
Hello,

am I right that in
p = new SomeClass(p)
the newly constructed object is initialized and therefore the p in the
brackets is used before the assignment of the new pointer to p takes place?
Yes.


Is it generally guaranteed that all side effects of an expression have taken
place before the expression's result is used as an operator argument or
function parameter?

A sequence point (which means that the side effects will have been applied)
occurs when a function is called and again when it returns. As a result the
answer to your question is yes.

However, be careful of the following sort of thing:

p = f(expression_a) + g(expression_b);

expression_a and expression_b are guaranteed to be consistant before their use as
a parameter in the respective functions, but it's possible that both expressions are
evaluated before either function is called (or they could be defered to the point of the
call). This makes expressions like:
p = f(++a) + g(++a)
undefined behavior, because it's possible that ++a is executed before the sequence
points inherent in the function calls.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top