473,396 Members | 2,029 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,396 software developers and data experts.

assigning to the reference returned by the vector [] operator crashesthe program! arghh

I am baffled. According to the C++ faq lite it is ok to use a reference
as an lvalue, and the subscript operator returns a reference. However,
when I run this program, it crashes!
I will go set up a different compiler and try again while I'm waiting
for a reply, but I'm really curious about why this is happening. I'm
using the mingw compiler that came with dev-c++ on an up to date Windows
2000 box. Any help would be greatly appreciated, thanks.

#include <vector>
int main()
{
std::vector<intvi;
vi[0] = 1; // Crashes for no apparent reason.
//vi.push_back(1); // Unacceptable workaround, but it doesn't crash.
return(0);
}
Feb 21 '07 #1
7 1743

Daniel je napisao:
I am baffled. According to the C++ faq lite it is ok to use a reference
as an lvalue, and the subscript operator returns a reference. However,
when I run this program, it crashes!
I will go set up a different compiler and try again while I'm waiting
for a reply, but I'm really curious about why this is happening. I'm
using the mingw compiler that came with dev-c++ on an up to date Windows
2000 box. Any help would be greatly appreciated, thanks.

#include <vector>
int main()
{
std::vector<intvi;
vi[0] = 1; // Crashes for no apparent reason.
//vi.push_back(1); // Unacceptable workaround, but it doesn't crash.
return(0);
}
Note: after constructing std::vector, it's size is 0 (zero). So,
assigning 1 to
first element is logical error; use push_back(), or resize vector to
your need.
Find on google some std::'any-container' references, for more info.

Feb 21 '07 #2
Daniel <ro******@telus.netwrote:
I am baffled. According to the C++ faq lite it is ok to use a reference
as an lvalue, and the subscript operator returns a reference. However,
when I run this program, it crashes!
I will go set up a different compiler and try again while I'm waiting
for a reply, but I'm really curious about why this is happening. I'm
using the mingw compiler that came with dev-c++ on an up to date Windows
2000 box. Any help would be greatly appreciated, thanks.

#include <vector>
int main()
{
std::vector<intvi;
At this point, vi has size() of 0.
vi[0] = 1; // Crashes for no apparent reason.
Since the size is 0, trying to access the first element (at index 0) is
an error.
//vi.push_back(1); // Unacceptable workaround, but it doesn't crash.
return(0);
}
One workaround is to declare vi with an initial size, like

std::vector<intvi(42);

This will create vi with 42 elements, each of which is
default-initialized.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Feb 21 '07 #3
On 21 Feb, 08:47, Daniel <ross9...@telus.netwrote:
I am baffled. According to the C++ faq lite it is ok to use a reference
as an lvalue, and the subscript operator returns a reference. However,
when I run this program, it crashes!
I will go set up a different compiler and try again while I'm waiting
for a reply, but I'm really curious about why this is happening. I'm
using the mingw compiler that came with dev-c++ on an up to date Windows
2000 box. Any help would be greatly appreciated, thanks.

#include <vector>
int main()
{
std::vector<intvi;
Creates an empty vector, i.e. no elements exist yet.
vi[0] = 1; // Crashes for no apparent reason.
Attempts to assign the value 1 to the first element of a vector with
no elements. That's the reason. The above two lines make as much sense
as

int* pi;
*pi = 1;
//vi.push_back(1); // Unacceptable workaround, but it doesn't crash.
That's not a workaround, it's a well-defined way of using a vector.
However, if puch_back is unaaceptable to you there are plenty of other
options. Read the documentation for std::vector. It has constructors
and members that allow you, for example, to create it with a certain
initial size, to create a vector as a copy of another sequence, to
create it with no elements but to reserve memoryfor a certain number
of elements to be created later.

Feb 21 '07 #4
Thanks Pasalic, Marcus and Gavin, for some reason I thought the space
was allocated automatically when I assigned to it, and didn't realize I
was making an assumption.
Feb 22 '07 #5
>
One workaround is to declare vi with an initial size, like

std::vector<intvi(42);

This will create vi with 42 elements, each of which is
default-initialized.
That only creates "room" for 42 slots, but after:
vi[0] = 1;
the size is still zero so watch out. The only 100% safe way is using
push_back(). If that's prohibitive performance-wise try std::deque (at
least in the gcc implementation is much faster).

Marius Lazer

Feb 23 '07 #6
JE
On Feb 22, 5:13 pm, "marius lazer" <marius.la...@gmail.comwrote:
One workaround is to declare vi with an initial size, like
std::vector<intvi(42);
This will create vi with 42 elements, each of which is
default-initialized.

That only creates "room" for 42 slots, but after:
vi[0] = 1;
the size is still zero so watch out. The only 100% safe way is using
push_back(). If that's prohibitive performance-wise try std::deque (at
least in the gcc implementation is much faster).

Marius Lazer
vi.size() is 42. What are you saying isn't safe, and what size are you
saying is zero?

Feb 23 '07 #7
On 23 fév, 02:13, "marius lazer" <marius.la...@gmail.comwrote:
One workaround is to declare vi with an initial size, like
std::vector<intvi(42);
This will create vi with 42 elements, each of which is
default-initialized.

That only creates "room" for 42 slots, but after:
vi[0] = 1;
the size is still zero so watch out. The only 100% safe way is using
push_back(). If that's prohibitive performance-wise try std::deque (at
least in the gcc implementation is much faster).

Marius Lazer
The called std::vector<constructor resizes the vector to the size
passed as a parameter, it does not just reserve it. As a consequence,
vi.size() is 42 after this call. Compare:

std::vector<intvi1; // vi1.size() == 0, vi1.capacity() == 0
vi1.resize(42); // vi1.size() == 42, , vi1.capacity() >= 42

std::vector<intvi2(42); // vi2.size() == 42, , vi2.capacity() >= 42

std::vector<intvi3; // vi3.size() == 0, vi3.capacity() == 0
vi3.reserve(42); // vi3.size() == 0; but vi3.capacity() >= 42

Regards,

-- Emmanuel Deloget

Feb 23 '07 #8

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

Similar topics

9
by: mjm | last post by:
Folks, Stroustrup indicates that returning by value can be faster than returning by reference but gives no details as to the size of the returned object up to which this holds. My question is...
3
by: klaas | last post by:
the following code gives rise to the beneath error message, only when a matrix object is instantiated as matrix<bool>, not with matrix<float>: /*returns a reference to the object at position...
14
by: Protoman | last post by:
Can I write: T i&=new T; Thanks!!!
4
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { }...
10
by: ma740988 | last post by:
I'm perusing source that quite frankly looks like a disaster in the making. This is a case though where return by reference gets muddled with my understand based on readings. struct bar {...
12
by: ypjofficial | last post by:
Hello All, I need to return a vector from a function.The vector is of int and it can contain as many as 1000 elements.Earlier I was doing //function definition vector<intretIntVector() {...
25
by: Sourav | last post by:
Suppose I have a code like this, #include <stdio.h> int *p; void foo(int); int main(void){ foo(3); printf("%p %d\n",p,*p);
5
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
6
by: silverburgh.meryl | last post by:
Hi, In one A.cpp file, I have defined a static array of JSFunctionSpec, like this: static JSFunctionSpec JProfFunctions = { {"JProfStartProfiling", JProfStartProfiling, 0, 0, 0 },...
10
by: flopbucket | last post by:
Hi, Is this legal? std::string foo() { std::string xyz = "FOO"; return xyz; }
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.