C++ STL checked/unchecked versions information
Checked/unchecked iterators versions
Checked iterators ensure that you do not overwrite the bounds of your container. The following symbols are for use with the checked iterators feature.
_SECURE_SCL
If defined as 1, unsafe iterator use causes a runtime error. If defined as 0, checked iterators are disabled. The exact behavior of the runtime error depends on the value of _SECURE_SCL_THROWS. The default value for _SECURE_SCL is 1, meaning checked iterators are enabled by default.
_SECURE_SCL_THROWS
If defined as 1, an out of range iterator use causes an exception at runtime. If defined as 0, the program is terminated by calling invalid_parameter. The default value for _SECURE_SCL_THROWS is 0, meaning the program will be terminated by default. Requires _SECURE_SCL to also be defined.
When _SECURE_SCL=1 is defined:
1. All standard iterators (vector::iterator, for example) are checked.
2. The checked form of an algorithm will be used, for standard functions with checked forms (see list below).
3. If an output iterator is a checked iterator:
a. You will get checked behavior on calls to the standard function (std::copy, for example).
b. You will get checked behavior on calls to a checked function (stdext::checked_copy, for example).
c. You will get checked behavior on calls to an unchecked function (stdext::unchecked_copy, for example).
4. If the output iterator is an unchecked iterator (an array, for example):
a. Calls to the standard function (std::copy, for example) will result in compiler warnings.
b. Calls to the checked function (stdext::checked_copy, for example) will result in compiler warnings.
c. You will get unchecked behavior on calls to an unchecked function (stdext::unchecked_copy, for example).
The following functions will generate a runtime error if there is an access outside the bounds of the container:
vector::operator[ ]
basic_string::operator[ ]
deque::operator[ ]
bitset::operator[ ]
valarray::operator[ ]
vector::front
queue::front
list::front
deque::front
vector::back
queue::back
list::back
deque::back
When _SECURE_SCL=0 is defined:
1. All standard iterators are unchecked (same behavior as specified by the C++ standard).
2. The unchecked form of a function will be used, for standard functions with checked forms (see list below).
3. If an output iterator is a checked iterator:
a. You will get checked behavior on calls to the standard function (std::copy, for example).
b. You will get checked behavior on calls to a checked function (stdext::checked_copy, for example).
c. You will get checked behavior on calls to an unchecked function (stdext::unchecked_copy, for example).
4. If an output iterator is an unchecked iterator:
a. You will get unchecked behavior on calls to the standard function (std::copy, for example).
b. Calls to a checked function (stdext::checked_copy, for example) will result in compiler warnings.
c. You will get unchecked behavior on calls to an unchecked function (stdext::unchecked_copy, for example).
A checked iterator refers to an iterator that will throw an exception or call invalid_parameter if you attempt to move past the boundaries of the container. For more information about invalid_parameter, see Parameter Validation. A checked algorithm enforces the use of a checked destination iterator. A checked algorithm will not pass compilation if passed an unchecked destination iterator. There are two iterator adaptors that support checked iterators:
1. checked_iterator Class
2. checked_array_iterator Class
The following algorithms enforce the use of a checked iterator as output iterator. This is useful when you want to compile with _SECURE_SCL=0, and where you identify some code where you want to enforce the use of checked iterators. The following algorithms are all defined in the stdext namespace.
checked_adjacent_difference
checked_copy
checked_copy_backward
checked_fill_n
checked_generate_n
checked_merge
checked_partial_sum
checked_remove_copy
checked_remove_copy_if
checked_replace_copy
checked_replace_copy_if
checked_reverse_copy
checked_rotate_copy
checked_set_difference
checked_set_intersection
checked_set_symmetric_difference
checked_set_union
checked_uninitialized_copy
checked_uninitialized_fill_n
checked_unique_copy
The following algorithms allow the use of an unchecked iterator as output iterator. This is useful when you want to compile with _SECURE_SCL=1, and where you want to selectively allow the use of unchecked iterators. The following algorithms are all defined in the stdext namespace.
unchecked_adjacent_difference
unchecked_copy
unchecked_copy_backward
unchecked_fill_n
unchecked_generate_n
unchecked_merge
unchecked_partial_sum
unchecked_remove_copy
unchecked_remove_copy_if
unchecked_replace_copy
unchecked_replace_copy_if
unchecked_reverse_copy
unchecked_rotate_copy
unchecked_set_difference
unchecked_set_intersection
unchecked_set_symmetric_difference
unchecked_set_union
unchecked_uninitialized_copy
unchecked_uninitialized_fill_n
unchecked_unique_copy
When compiling with _SECURE_SCL 1, a runtime error will occur if you attempt to access an element outside the bounds of the container with the indexing operator of certain classes.
C++ algorithm checked_copy() version code example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: none
To do: Using the checked_copy() which is same as copy(), but enforces the use of a checked iterator as output iterator
To show: How to use the C++ algorithm, checked_copy() version to enforce the use of a checked iterator as output iterator in C++ programming
// C++ STL algorithm checked_copy() version
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
using namespace stdext;
int main(void)
{
// vector containers
vector <int> vec1, vec2;
// vector iterators
vector <int>::iterator Iter1, Iter2;
int i, ii;
for (i = 0; i <= 5; i++)
vec1.push_back(10 * i);
for (ii = 0; ii <= 10; ii++)
vec2.push_back(3 * ii);
cout<<"vec1 = ";
for (Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
cout<<"\nvec2 = ";
for (Iter2 = vec2.begin(); Iter2 != vec2.end(); Iter2++)
cout<<*Iter2<<" ";
cout<<endl;
// to copy the first 3 elements of vec1 into the middle of vec2
checked_copy(vec1.begin(), vec1.begin() + 3, vec2.begin() + 4);
cout<<"\nvec2 with copy the first 3 elements of vec1 into the middle of vec2 = ";
for (Iter2 = vec2.begin(); Iter2 != vec2.end(); Iter2++)
cout<<*Iter2<<" ";
cout<<endl;
// to shift the elements inserted into vec2 two positions to the left
checked_copy(vec2.begin()+4, vec2.begin() + 7, vec2.begin() + 2);
cout<<"\nvec2 with shifted elements inserted into vec2 two positions to the left = ";
for (Iter2 = vec2.begin(); Iter2 != vec2.end(); Iter2++)
cout<<*Iter2<<" ";
cout<<endl;
return 0;
}
Output example:
vec1 = 0 10 20 30 40 50
vec2 = 0 3 6 9 12 15 18 21 24 27 30
vec2 with copy the first 3 elements of vec1 into the middle of vec2 = 0 3 6 9 0 10 20 21 24 27 30
vec2 with shifted elements inserted into vec2 two positions to the left = 0 3 0 10 20 10 20 21 24 27 30
Press any key to continue . . .