modifying a vector (STL) in a function

Discussion in 'C++' started by clinisbut, Jan 11, 2008.

  1. clinisbut

    clinisbut Guest

    I'm trying to understand what's wrong with this:

    void getAllData( std::vector<unsigned char> &char_array )
    {
    char_array.clear();
    char_array.reserve( 11 );

    char_array[0] = 'A'; //<----No error compilation, but no
    effect!!
    }

    and then I call this function:

    std::vector<unsigned char> char_array;
    getAllData( char_array );

    But if I do this:
    void getAllData( std::vector<unsigned char> &char_array )
    {
    char_array.clear();
    char_array.reserve( 11 );

    char_array.push_back('A'); //Works
    }

    It works good, but when I try to read some element with [] operator
    doesn't works...
    What's the correct way to deal with a vector inside a function???

    PD: What's the diference between using & and * in function header's
    parameter list?
     
    clinisbut, Jan 11, 2008
    #1
    1. Advertising

  2. clinisbut

    yaoyi Guest

    On Jan 11, 8:10 pm, clinisbut <> wrote:
    > I'm trying to understand what's wrong with this:
    >
    > void getAllData( std::vector<unsigned char> &char_array )
    > {
    > char_array.clear();
    > char_array.reserve( 11 );

    the char_array is empty!
    you can use char_array.resize(11); instead
    >
    > char_array[0] = 'A'; //<----No error compilation, but no
    > effect!!
    >
    > }
     
    yaoyi, Jan 11, 2008
    #2
    1. Advertising

  3. clinisbut

    Guest

    clinisbut wrote:

    > I'm trying to understand what's wrong with this:
    >
    > void getAllData( std::vector<unsigned char> &char_array )
    > {
    > char_array.clear();
    > char_array.reserve( 11 );
    >
    > char_array[0] = 'A'; //<----No error compilation, but no effect!!


    Actually, you have an out-of-bounds error and the code has undefined
    behavior.

    > }
    >
    > and then I call this function:
    >
    > std::vector<unsigned char> char_array;
    > getAllData( char_array );
    >
    > But if I do this:
    > void getAllData( std::vector<unsigned char> &char_array )
    > {
    > char_array.clear();
    > char_array.reserve( 11 );
    >
    > char_array.push_back('A'); //Works
    > }


    This is a correct way to fill a vector.


    > It works good, but when I try to read some element with [] operator
    > doesn't works...


    operator[] and the at() function only allow you to access vector elements
    that are already stored in the container; calling them will not magically
    enlarge the vector (as opposed to std::map, where this kind of magic
    happens). Thus, you have to populate the vector _before_ you can access its
    elements. The push_back() function is the way to go.


    > What's the correct way to deal with a vector inside a function???


    That all of this happens inside a function is immaterial.


    > PD: What's the diference between using & and * in function header's
    > parameter list?


    & denotes a reference parameter and * denotes a pointer.


    Best

    Kai-Uwe Bux
     
    , Jan 11, 2008
    #3
  4. clinisbut

    clinisbut Guest

    Oh... I think i confused reserve() with resize()...
    thank you!
     
    clinisbut, Jan 11, 2008
    #4
    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. Dennis
    Replies:
    1
    Views:
    2,600
    Dennis
    Jun 7, 2004
  2. CD
    Replies:
    2
    Views:
    810
    Victor Bazarov
    Oct 5, 2004
  3. Replies:
    2
    Views:
    428
  4. Replies:
    8
    Views:
    1,960
    Csaba
    Feb 18, 2006
  5. Luca Risolia

    STL map to STL vector

    Luca Risolia, Jan 13, 2014, in forum: C++
    Replies:
    32
    Views:
    391
    Seungbeom Kim
    Jan 18, 2014
Loading...

Share This Page