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

D

Daniel

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<int> vi;
vi[0] = 1; // Crashes for no apparent reason.
//vi.push_back(1); // Unacceptable workaround, but it doesn't crash.
return(0);
}
 
P

pasalic.zaharije

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<int> vi;
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.
 
M

Marcus Kwok

Daniel said:
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<int> vi;

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<int> vi(42);

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

Gavin Deane

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<int> vi;
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.
 
D

Daniel

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

marius lazer

One workaround is to declare vi with an initial size, like

std::vector<int> vi(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
 
J

JE

One workaround is to declare vi with an initial size, like
std::vector<int> vi(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?
 
E

Emmanuel Deloget

One workaround is to declare vi with an initial size, like
std::vector<int> vi(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<int> vi1; // vi1.size() == 0, vi1.capacity() == 0
vi1.resize(42); // vi1.size() == 42, , vi1.capacity() >= 42

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

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

Regards,

-- Emmanuel Deloget
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top