stl vector assignment question

T

txtamil2

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
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.
 
A

Andre Kostur

(e-mail address removed) wrote in @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.
 

nmi

Joined
Jul 6, 2007
Messages
13
Reaction score
0
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).
 
B

BobR

Andre Kostur said:
(e-mail address removed) 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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top