Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Visual C++: C4996 Warning on Copy with Array Parameters

📅 2010-Aug-30 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ checked, iterators, visual c++, warnings ⬩ 📚 Archive

Visual C++ throws a C4996 warning if std::copy is called with array parameters (instead of the conventional iterator parameters). For example, if you have code of the form:

std::copy( arr0, arr0 + 5, arr1 );

where arr0 and arr1 are pointers to arrays or valid memory locations.

The C4996 warning is of the form:

warning C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'

Visual C++ uses checked iterators by default everywhere for security reasons. This warning is a manifestation of that design decision. There are a few ways to deal with it:

#include <iterator>
std::copy( arr0, arr0 + 5, stdext::checked_array_iterator<int*>( arr1, 5 ) );
stdext::unchecked_copy( arr0, arr0 + 5, arr1 );