• Почему происходит утечка памяти?

    @encyclopedist
    Согласно стандарту, порядок вычислений может быть разным, в том числе таким:
    tmp1 = new int(42);
    tmp2 = seed();
    tmp3 = std::shared_ptr<int>(tmp1);
    foo(tmp3, tmp2);

    В таком случае, если seed бросает исключение, возникает утечка памяти.
    Именно для избежания этой проблемы в стандарте предусмотрена вспомогательная функция make_shared. Исправленный вариант вашего кода будет выглядеть так:
    foo(std::make_shared<int>(42), seed());
    Помимо безопасности, также можно получить повышение производительности, поскольку make_shared может сделать одно выделение памяти вместо двух.
    Доп ссылки:
    herbsutter.com/2013/05/29/gotw-89-solution-smart-p...
    stackoverflow.com/questions/20895648/difference-in...

    Update
    Насчёт порядка вычислений стандарт говорит следующее:
    [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.

    Таким образом, вычисление этих tmp1 и tmp2 в нашем случае является неупорядоченным (unsequenced).
    Ответ написан
    3 комментария