Am 30.01.15 um 19:23 schrieb Paul Rubin:
> Michael Torrie <torr...@gmail.com> writes:
>> Follow basic [C++] rules and 99% of segfaults will never happen and
>> the majority of leaks will not happen either.
> 
> That is a safe and simple approach, but it works by copying data all
> over the place instead of passing pointers, resulting in performance
> loss.  

This "performance loss" is partly a myth. Consider the following code,
assuming it is compiled using a recent (C++11) compiler

====================
#include <vector>

std::vector<double> compute() {
        const size_t N=100000;
        std::vector<double> result(N);
        for (size_t i=0; i<N; i++) {
                result[i]=2*i;
        }
        return result;
}

int main() {
        auto s = compute();
        // print it or whatever
        return 0;
}

=========================


At first, it may seem that this code copies the big vector twice: Once
into a temporary return value, once into the automatic variable s. This
is not the case, once for the move constructors in C++11 and second for
return value optimization, some years already in the compilers. Instead,
the vector is constructed directly into the place where the main
functinos expects it to be.

        Christian

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to