I have the following code that copies a std::vector a into another std::vector b, but with 2 as the starting index. As both vectors are of length 4, this results in an out-of-bounds write. I would like to let this code throw an exception, but how can I do this? The code below crashes with a segmentation fault.
#include <vector>
#include <iostream>
int main()
{
std::vector<double> a = {1, 2, 3, 4};
std::vector<double> b(4);
try
{
std::transform(a.begin(), a.begin()+4, b.begin()+2,
[](const double d) { return d; });
}
catch (std::exception& e)
{
std::cout << "EXCEPTION: " << e.what() << std::endl;
return 1;
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire