array, array, vector, yay

master
neingeist 11 years ago
parent 86c0368a04
commit 133098604d

@ -6,8 +6,7 @@ void classic() {
int array[2]; int array[2];
array[0] = 1; array[0] = 1;
array[1] = 2; array[1] = 2;
// array[3] = 3; <- clang++ -Warray-bounds warns about this array[3] = 3; // <- clang++ -Warray-bounds warns about this
// array[4] = 4; <- clang++ -Warray-bounds warns about this
int n = 10; int n = 10;
std::cout << array[3] << std::endl; // no warning! std::cout << array[3] << std::endl; // no warning!
@ -15,42 +14,53 @@ void classic() {
std::cout << array[n-1] << std::endl; // no warning! std::cout << array[n-1] << std::endl; // no warning!
} }
// FIXME
void std_array() { void std_array() {
std::array<int, 2> array; std::array<int, 2> array;
array[0] = 1; array[0] = 1;
array[1] = 2; array.at(1) = 2;
array[3] = 3; // XXX <- clang++ -Warray-bounds warns about this
// array[4] = 4; <- clang++ -Warray-bounds warns about this
int n = 10; // whoopsie
std::cout << array[3] << std::endl; // no warning! array[2] = 3;
std::cout << array[4] << std::endl; // no warning!
std::cout << array[n-1] << std::endl; // no warning! for(auto &e: array) {
std::cout << e << " ";
}
std::cout << std::endl;
try {
std::cout << array.at(2) << std::endl;
} catch (std::out_of_range e) {
std::cout << "cool, got std::out_of_range." << std::endl;
}
} }
// FIXME
void std_vector() { void std_vector() {
std::vector<int> vector; std::vector<int> vector(1);
vector[0] = 1; vector[0] = 1;
vector[1] = 2; try {
vector.at(2) = 999;
} catch (std::out_of_range e) {
std::cout << "cool, got std::out_of_range." << std::endl;
}
vector.resize(10);
vector.at(2) = 3;
vector[1] = 2; // should now work
// vector[3] = 3; // SEGFAULT // vector[3] = 3; // SEGFAULT
// vector[4] = 4; <- clang++ -Warray-bounds warns about this
int n = 10; for(auto &e: vector) {
// std::cout << vector[3] << std::endl; // no warning! std::cout << e << " ";
// std::cout << vector[4] << std::endl; // no warning! }
// std::cout << vector[n-1] << std::endl; // no warning! std::cout << std::endl;
} }
int main() { int main() {
std::cout << "classic()" << std::endl; std::cout << "== classic()" << std::endl;
classic(); classic();
std::cout << "std_array()" << std::endl; std::cout << "== std_array()" << std::endl;
std_array(); std_array();
std::cout << "std_vector()" << std::endl; std::cout << "== std_vector()" << std::endl;
std_vector(); std_vector();
return 0; return 0;

Loading…
Cancel
Save