473,320 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

stl vector assignment question

I have declared a vector like this:
typedef vector <intmyvec;

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 '07 #1
3 1674
On 2007-07-19 17:56, tx******@tx.rr.com wrote:
I have declared a vector like this:
typedef vector <intmyvec;

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
Jul 19 '07 #2
tx******@tx.rr.com wrote in news:1184860577.420958.202990
@n60g2000hse.googlegroups.com:
I have declared a vector like this:
typedef vector <intmyvec;

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.
Jul 19 '07 #3

Andre Kostur <nn******@kostur.netwrote in message...
tx******@tx.rr.com wrote in googlegroups
I have declared a vector like this:
typedef vector <intmyvec;

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
Jul 20 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: zhou | last post by:
Hi there, I am expecting that the following assignment will invoke assignment operator on vector (since myFunc() returns a const vector & type), which makes a copy of the vector returned from...
9
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
5
by: SB | last post by:
I have an assignment where we are supposed to mimic a scheduler routine of an OS. We are supposed to schedule some processes, an each process has the following attributes... - process id -...
8
by: Hamish | last post by:
I havea program which on execution gives unpredictable behaviour (it shouldn't). In trying to track down the problem, I'm wondering if there is a difference between these two ways of filling a...
14
by: Michael Sgier | last post by:
Hello If someone could explain the code below to me would be great. // return angle between two vectors const float inline Angle(const CVector& normal) const { return acosf(*this % normal); }...
10
by: Piotr | last post by:
I have a class 'Statistics' which has a private attribute ' vector<int>* _x;' And in the destructor of the Statistics, I have this code to free the memory: Statistics::~Statistics() { if...
23
by: sidney | last post by:
Hi all, I am trying to make a vector containing objects the have a reference member. However, as soon as I try to push_back an element into this vector, g++ balks at the fact that it needs to...
1
by: Raghuram N K | last post by:
Hi, Following program compiles and executes successfully in windows with DevCPP compiler. When I compile the same in Linux with 'g++323' compiler I get following assignment error: cannot...
3
by: Eric Lilja | last post by:
Hello, consider the following assignment and my code for it: /* Write a program that does the following: * Integer numbers shall be read from a textfile and stored in a std::vector. The name...
7
by: zl2k | last post by:
hi, all What is the default size of a stl vector? Suppose I have integers needs to store in a vector and I know the max number of integer is, say, 1000. Will it be more efficient that I first...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.