Re: Returning valarrays

Discussion in 'C++' started by E. Robert Tisdale, Jul 1, 2003.

  1. Harald Grossauer wrote:

    > If I return a valarray as a result of a function
    > is then the whole valarray copied onto the stack?


    No. Not if you have a good optimizing C++ compiler.

    > I.E. if I do a lot of operations on classes containing valarrays,
    > say like:
    >
    > v1 = v2*(v3+v4/2.0);
    > return v1;
    >
    > where v* are instances of
    >
    > class myclass {
    > ....// some other variables
    > valarray<float> ...;
    > }
    >
    > is then a complete valarray copied for every intermediate result of the
    > right hand side (i.e. for each call of the copy constructor)?
    >
    > What about the explicit return statement?
    >
    > Is the implementation of valarray "clever" enough
    > to prevent unnecessary copying by itself?


    Yes.

    Suppose that you have a function

    myclass f(void) {
    myclass t;
    // modify t
    return t;
    }

    A good optimizing C++ compiler should recognize t as an alias
    for the return value, call the default constructor
    to initialize the return value instead of creating
    a local copy of t, copying it to the return value
    then call the destructor for t.
    This is called the Named Return Value Optimization (NRVO).

    When you write

    myclass x = f();

    s good optimizing C++ compiler will allocate storage for x
    and pass a reference (or a pointer) to x as a hidden argument to f --
    the compiler will write the same code that it would write for

    myclass& f(myclass& t) {
    // initialize t with the default constructor
    // modify t
    return t;
    }

    The copy constructor is *never* called!

    Using expression classes, you can get the C++ compiler
    to *fuse* two or more operations for you
    according to Bjarne Stroustrup, "The C++ Programming Language",
    Chapter 22 Numerics, Section 4 Vector Arithmetic,
    Subsection 7 Temporaries, Copying, and Loops, pages 675-7.

    Using expression class templates, you can get the C++ compiler
    to generate your expression classes automatically
    according to Todd Veldhuizen, "The Blitz++ Library"

    http://www.oonumerics.org/blitz/
     
    E. Robert Tisdale, Jul 1, 2003
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tom_usenet

    Re: Returning valarrays

    tom_usenet, Jul 1, 2003, in forum: C++
    Replies:
    0
    Views:
    401
    tom_usenet
    Jul 1, 2003
  2. Michael Aramini

    C++ STL valarrays vs. vectors

    Michael Aramini, Sep 2, 2003, in forum: C++
    Replies:
    10
    Views:
    13,358
    vrambati
    Nov 3, 2003
  3. Sebastian Thiebes

    valarrays and regular arrays

    Sebastian Thiebes, Jun 9, 2004, in forum: C++
    Replies:
    3
    Views:
    480
    Marc Schellens
    Jun 10, 2004
  4. Gerard Kramer
    Replies:
    9
    Views:
    398
    Gerard Kramer
    Jul 10, 2006
  5. Replies:
    5
    Views:
    431
    Lionel B
    Feb 23, 2007
Loading...

Share This Page