Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: [OT] fortran lib which provide python like data type

14 views
Skip to first unread message

Christian Gollwitzer

unread,
Jan 31, 2015, 3:27:19 AM1/31/15
to
Am 30.01.15 um 19:23 schrieb Paul Rubin:
> Michael Torrie <tor...@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

0 new messages