Answer the question
In order to leave comments, you need to log in
Why is there a memory leak?
Memory leaks are known to occur when seed() throws an exception.
It's just that I don't quite understand why.
void foo( std::shared_ptr< int > p, int init ) {
*p = init;
}
int seed() {
//some actions
}
foo( std::shared_ptr< int >( new int( 42 ) ), seed() );
Answer the question
In order to leave comments, you need to log in
According to the standard, the order of calculations can be different, including the following:
tmp1 = new int(42);
tmp2 = seed();
tmp3 = std::shared_ptr<int>(tmp1);
foo(tmp3, tmp2);
[intro.execution] Paragraph 15:
When calling a function (whether or not the function is inline), every value computation and side effect associated with any argument expression, or with the postfix expression designating the called function, is sequenced before execution of every expression or statement in the body of the called function. [ Note: Value computations and side effects associated with different argument expressions are unsequenced. — end note] Every evaluation in the calling function (including other function calls) that is not otherwise specifically sequenced before or after the execution of the body of the called function is indeterminately sequenced with respect to the execution of the called function.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question