发现这个代码,在做代码审查:传递指针代替迭代器的std ::复制
std::array<uint, 10> expArray2 = {92,130,169,951,634,187,377,233,865,944};
copy(&expArray2[0], &expArray2[10], back_inserter(expected));
^~~~Is this Undefined behavior as ArrayIndexOutOfBoundAccess?
它是一样的(我的建议):
std::copy (expArray2, expArray2+10, expected.begin());
我的同事说,1。 )都是相同的2.)没有未定义的行为?
我向他解释,指针和迭代器是两个different的东西,用std::copy签名一起:
template <class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result);
可能有人请确认/这个给我解释如果我错了随时随地?
指针是一种迭代器。甚至在您提供的链接中提到了这一点。 – user2357112
是的,但我主要关心的是arrayIndexOutOfBoundAccess。 –
似乎这个问题的核心是关于'&expArray2 [10]'的表达。在这种情况下,这看起来相关:http://stackoverflow.com/questions/988158/take-the-address-of-a-one-past-the-end-array-element-via-subscript-legal-by- – TheUndeadFish