Difference between std :: tr1 :: array and boost :: array

I got the impression that std :: tr1 :: array was the same as boost :: because it would throw an exception when accessing the index outside the bounds. In fact, I looked at the headline, and it looks just as good. Can someone explain why the following code leads to a bus error (gcc version 4.0.1 (Apple Inc., build 5465)) and segfault on gcc 4.1.2?

Thank.

#include <exception>
#include <iostream>
#include <string>
#include <tr1/array>
#include <boost/array.hpp>

int main()
{
    // boost::array<std::string, 3> arr;
    std::tr1::array<std::string, 3> arr;
    try
    {
        arr.at( 0 ) = "one";
        arr.at( 1 ) = "two";
        arr.at( 2 ) = "three";
        arr.at( 3 ) = "nogood";
    }
    catch ( const std::exception& e )
    {
        std::cout << "exception: " << e.what() << std::endl;
    }
    return 0;
}
+3
source share
2 answers

This may be a bug in your particular installed version of the compiler. Here's what GCC does for your code on my system (Linux x86-64):

$ g++-4.1.2 test.cpp -o test
$ ./test
exception: array::_M_at
$ g++-4.3.5 test.cpp -o test
$ ./test
exception: array::at
$ g++-4.4.4 test.cpp -o test
$ ./test
exception: array::at
$ g++-4.5.0 test.cpp -o test
$ ./test
exception: array::at

, , , , , , GCC 4.1.2, . - ? Valgrind .

+3

(array<std::string, 3>), 4- (arr.at(3)). boost:: array . tr1:: array, () ++ 0x Standard, , array::operator[]() . , tr1:: array boost:: array ( , ). " " / "segfault" .

0

Source: https://habr.com/ru/post/1751382/


All Articles