Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > stl vector assignment question

Reply
Thread Tools

stl vector assignment question

 
 
txtamil2@tx.rr.com
Guest
Posts: n/a
 
      07-19-2007
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

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      07-19-2007
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
 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      07-19-2007
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.
 
Reply With Quote
 
nmi nmi is offline
Junior Member
Join Date: Jul 2007
Posts: 13
 
      07-19-2007
Quote:
Originally Posted by
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
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).
 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      07-20-2007

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Vector of STL maps versus Vector of objects amolpan@gmail.com C++ 2 07-26-2005 10:16 PM
Automatic Conversion of STL Containers: e.g. from vector<derived*> to vector<base*> CD C++ 2 10-05-2004 02:29 PM
STL algorithm problem vector<vector<double> > and find Dennis C++ 1 06-07-2004 10:09 PM
why vector assignment operator not invoked on const vector& ? zhou C++ 9 09-02-2003 08:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57