stl vector assignment question

Discussion in 'C++' started by txtamil2@tx.rr.com, Jul 19, 2007.

  1. Guest

    I have declared a vector like this:
    typedef vector <int> myvec;

    1. myvec vec1;
    2. vec1.push_back(4);
    3. vec1[100] = 5;

    The question is how come line 3 works (no crash). But when I look at
    the size, the size is 1.

    Thanks.
    Siva
     
    , Jul 19, 2007
    #1
    1. Advertising

  2. On 2007-07-19 17:56, wrote:
    > I have declared a vector like this:
    > typedef vector <int> myvec;
    >
    > 1. myvec vec1;
    > 2. vec1.push_back(4);
    > 3. vec1[100] = 5;
    >
    > The question is how come line 3 works (no crash). But when I look at
    > the size, the size is 1.


    Because you are lucky. Trying to access an element that does not exist
    is undefined behaviour and while things might look like they work you
    can get yourself into deep trouble with this kind of code (since you
    could be fiddling with memory belonging to something else). If unsure if
    the element exist or not use vec1.at(100) instead.

    --
    Erik Wikström
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jul 19, 2007
    #2
    1. Advertising

  3. Andre Kostur Guest

    wrote in news:1184860577.420958.202990
    @n60g2000hse.googlegroups.com:

    > I have declared a vector like this:
    > typedef vector <int> myvec;
    >
    > 1. myvec vec1;
    > 2. vec1.push_back(4);
    > 3. vec1[100] = 5;
    >
    > The question is how come line 3 works (no crash). But when I look at
    > the size, the size is 1.


    Because it's Undefined Behaviour. You indexed off of the end of the array.
    If you want checked accesses, use:

    myvec vect1;
    vec1.push_back(4);
    vec1.at(100) = 5;


    This would cause at() to thrown an exception.
     
    Andre Kostur, Jul 19, 2007
    #3
  4. nmi

    Joined:
    Jul 6, 2007
    Messages:
    13
    Pure chance. You are referencing some false address and depending on the memory layout this can cause

    • immediate crash
    • a crash later
    • false behaviour
    • or nothing at all.

    The same situation like here:

    int t[3];

    t[100] = 999;

    You might write to a memory-location that your process is not allowed to write to (causing immediate crash) or you might overwrite some variable(s) of yours (crash later or funny results) or an unused memory-block (no effect).
     
    nmi, Jul 19, 2007
    #4
  5. BobR Guest

    Andre Kostur <> wrote in message...
    > wrote in googlegroups
    > > I have declared a vector like this:
    > > typedef vector <int> myvec;
    > >
    > > 1. myvec vec1;
    > > 2. vec1.push_back(4);
    > > 3. vec1[100] = 5;
    > >
    > > The question is how come line 3 works (no crash). But when I look at
    > > the size, the size is 1.

    >
    > Because it's Undefined Behaviour. You indexed off of the end of the

    array.
    > If you want checked accesses, use:
    >
    > myvec vect1;
    > vec1.push_back(4);


    // Add:
    vec1.resize( 101 );

    > vec1.at(100) = 5;
    >
    > This would cause at() to thrown an exception.


    Now it's a 'maybe'. <G>

    --
    Bob R
    POVrookie
     
    BobR, Jul 20, 2007
    #5
    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. zhou
    Replies:
    9
    Views:
    483
    Andrey Tarasevich
    Sep 2, 2003
  2. Dennis
    Replies:
    1
    Views:
    2,623
    Dennis
    Jun 7, 2004
  3. CD
    Replies:
    2
    Views:
    824
    Victor Bazarov
    Oct 5, 2004
  4. Replies:
    8
    Views:
    1,998
    Csaba
    Feb 18, 2006
  5. Luca Risolia

    STL map to STL vector

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

Share This Page