c++ - Why does erasing End-Iterator not give Segmentation Fault with shared_ptr? -


the following code runs without throwing segemntation fault error. however, call vec.erase erases last element, i.e. removes "pointer 1", reduces size of vec 1 , leaves "pointer 0" behind.

std::vector<std::shared_ptr<int>> vec; vec.push_back(std::make_shared<int>(0)); vec.push_back(std::make_shared<int>(1));  vec.erase(vec.end());  std::cout << vec.size() << std::endl 

the same code int throws segmentation fault error, excpected.

what reasons behind this?

ps: tested gcc 5.2.1

the same code int throws segmentation fault error, expected.

your expectation wrong. undefined behavior not guarantee program crash. surprise behavior undefined.


Comments