vico
vico

Reputation: 18175

Creating vector from c-array

Trying to make vector from part(5 elements) of c-array.

const static int size = 10;
cout << "vector from C-array: " << endl;
int ia[size] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
vector<int> vi2(ia, ia + size-5);

But when I try enumerate vector items using whole length of c-array I got all items like vector would be initialized like vector<int> vi2(ia, ia + size-5); . No exception or error that it goes out of range.

    for(int i = 0; i < size; i++) {
    cout << i << ":" << vi2[i] << " ";
}

In output:

0:1 1:2 2:3 3:4 4:5 5:6 6:7 7:8 8:9 9:10 

Why vector initialization is using second param that describes pointer to array end if it doesn't uses it?

Upvotes: 1

Views: 449

Answers (4)

juanchopanza
juanchopanza

Reputation: 227370

You are accessing your vector out of bounds. This is undefined behaviour. There is no requirement that an exception be thrown.

You must not go beyond vi2.size()-1.

for(int i = 0; i < vi2.size(); ++i) 
{
  std::cout << i << ":" << vi2[i] << " ";
}

Upvotes: 3

user529758
user529758

Reputation:

The [] operator doesn't do any bounds checking (similarly to the [] operator in C and C++ which works with raw arrays).

If you want an exception to be thrown, then use the at member function:

std::vector<int> v { 1, 2, 3, 4, 5 };
int invalidInt = v.at(5); // hell breaks lose, says the runtime

Upvotes: 2

Zeta
Zeta

Reputation: 105876

If you really want an exception to be thrown when you're out of bounds, use .at():

for(int i = 0; i < size; i++) {
    cout << i << ":" << vi2.at(i) << " ";
}

That being said, better use the actual size of vi2 (vi2.size()) instead.

Upvotes: 1

John Kugelman
John Kugelman

Reputation: 361556

C++ doesn't throw an exception the way Java does if you access an out of bounds index. It's undefined behavior, and you simply must not do it.

for(int i = 0; i < vi2.size(); i++) {
    cout << i << ":" << vi2[i] << " ";
}

See A Guide to Undefined Behavior in C and C++:

In a safe programming language, errors are trapped as they happen. Java, for example, is largely safe via its exception system. In an unsafe programming language, errors are not trapped. Rather, after executing an erroneous operation the program keeps going, but in a silently faulty way that may have observable consequences later on.

Upvotes: 1

Related Questions