Fun with valarray.

Discussion in 'C++' started by Steven T. Hatton, Oct 18, 2004.

  1. Examine the following code before you compile and run it. What do you
    expect to happen? Is what you expected consistent with the result of
    running it?

    #include <valarray>
    #include <iostream>

    using std::valarray;
    using std::slice;
    using std::cout;
    using std::eek:stream;

    valarray<size_t> makeValarray(const size_t& size) {
    valarray<size_t> va(size);
    for(size_t i = 0; i < va.size(); i++) { va = i + 1; }
    return va;
    }

    template<typename T>
    ostream& printValarray(const valarray<T>& va, ostream& out=cout) {
    for(size_t i = 0; i < va.size(); i++) {
    if(i){ out << ","; }
    out << va;
    }
    return out << "\n";
    }


    valarray<size_t> nibble(const valarray<size_t>& va) {
    return va[slice(1, va.size() - 1, 1)];
    }

    void spit() {
    valarray<size_t> va = makeValarray(16);
    while(va.size() > 1) {
    printValarray(va);
    va = nibble(va);
    }
    }

    int main()
    {
    spit();
    return 0;
    }

    --
    "If our hypothesis is about anything and not about some one or more
    particular things, then our deductions constitute mathematics. Thus
    mathematics may be defined as the subject in which we never know what we
    are talking about, nor whether what we are saying is true." - Bertrand
    Russell
     
    Steven T. Hatton, Oct 18, 2004
    #1
    1. Advertising

  2. Steven T. Hatton

    Markus Moll Guest

    Hi

    On Mon, 18 Oct 2004, Steven T. Hatton wrote:

    > Examine the following code before you compile and run it. What do you
    > expect to happen? Is what you expected consistent with the result of
    > running it?


    Hm... I hadn't expected it. But then, I have virtually no experience with
    valarrays. A short search told me that your code results in undefined
    behaviour, so you shouldn't expect too much either :)

    > va = nibble(va);


    Assignment to a valarray of different size is undefined.

    Keep in mind that design decisions for valarray were made with respect to
    efficiency. Furthermore, references to elements of a valarray remain valid
    until resize is called or the lifetime of the valarray ends. Therefore
    valarrays are _never_ implicitly resized.

    You will need a temporary variable here:

    valarray<size_t> va_tmp( nibble(va) );
    va.resize(va_tmp.size());
    va = va_tmp;


    Markus
     
    Markus Moll, Oct 18, 2004
    #2
    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. Andy Fish
    Replies:
    65
    Views:
    1,812
    Mabden
    May 18, 2004
  2. Glen Low
    Replies:
    3
    Views:
    991
    Glen Low
    Jun 26, 2003
  3. Jim West
    Replies:
    2
    Views:
    514
    Jim West
    Dec 23, 2003
  4. dolphin
    Replies:
    4
    Views:
    333
    Jorgen Grahn
    Aug 25, 2007
  5. er
    Replies:
    2
    Views:
    533
Loading...

Share This Page