Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual C++: How is checked_array_iterator useful?

On compiling code at Warning Level 4 (/W4), I get C4996 warnings on std::copy() calls whose parameters are C arrays (not STL containers like vectors). The recommended solution to fix this seems to be to use stdext::checked_array_iterator.

What is the use of stdext::checked_array_iterator? How does it work?

Why does it not give any compile warning on this piece of erroneous code compiled under Visual C++ 2010?:

#include <algorithm>
#include <iterator>
using namespace std;

int main()
{
    int arr0[5] = {100, 99, 98, 97, 96};
    int arr1[3];
    copy( arr0, arr0 + 5, stdext::checked_array_iterator<int*>( arr1, 3 ) );

    return 0;
}
like image 268
Ashwin Nanjappa Avatar asked Aug 30 '10 06:08

Ashwin Nanjappa


1 Answers

This page, Checked Iterators, describe how it works, but this quote sums it up: Checked iterators ensure that you do not overwrite the bounds of your container.

So if you go outside the bounds of the iterator it'll either throw and exception or call invalid_parameter.

like image 97
Hans Olsson Avatar answered Oct 29 '22 10:10

Hans Olsson