Checked iterator is an iterator with the out-of-boundary check function, and will trigger error processing during runtime when an out-of-boundary check is performed (call an invalid parameter processing routine or throw an exception ). Vc has supported checked iterator since vs2005. In addition, VC also supports debug iterator and has more inspection functions, which will not be discussed here.
Part one compilation
Checked iterator ensures that the iterator does not cross-border {
Tagshow (Event)
} "> Access (if a cross-border access occurs, it will be processed accordingly ). If a non-checked iterator is used where checked iterator is required, a compilation warning c4996 (level 3) is generated during compilation ). To disable this warning, you can define macro _ scl_secure_no_warnings.
- # DEFINE _ scl_secure_no_warning
Copy code
You can enable or disable the checked iterator check function by setting the macro _ secure_cl value. If macro _ secure_scl is defined as 1, the checked iterator check function is enabled. If macro _ secure_scl is defined as 0, the checked iterator check function is disabled. If the checked iterator discovers that the iterator is out of bounds, a runtime error is triggered. The specific behavior of this runtime error is related to the macro _ secure_scl_throws value. By default, the value of _ secure_scl is 1. (Note: The enable and disable of the checked iterator function and the Enable and disable of the debug iterator function do not affect each other)
The value of macro _ secure_scl_throws determines the method in which the checked iterator reports an error when detecting an out-of-bounds operation. If _ secure_scl_throws is defined as 1, the checked iterator will throw an exception STD: out_of_rangce when it discovers an out-of-bounds event. If _ secure_scl_throws is defined as 0, the invalid parameter processing routine of the CRT will be called (the default is to terminate {
Tagshow (Event)
} "> Program ). The default value of macro _ secure_scl_throws is 0. Because of the invalid CRT parameter processing routines, You can {
Tagshow (Event)
} "> Settings, so the program can change the default termination behavior. (Note: _ secure_scl_throws takes effect only when _ secure_scl is defined as 1)
The sample code is as follows:
- // Cl/ehstest1.cpp
- # Include <vector>
- # Include <iostream>
- Using namespace STD;
- Void myinvalidparameterhandler (const wchar_t * expression,
- Const wchar_t * function,
- Const wchar_t * file,
- Unsigned int line,
- Uintptr_t preserved)
- {
- Cout <"Just ignore it" <Endl;
- }
- Int main ()
- {
- Vector <int> V;
- V. push_back (11 );
- V. push_back (22 );
- Cout <V [1] <Endl;
- _ Set_invalid _{
Tagshow (Event)
} "> Parameter_handler (myinvalidparameterhandler );
- Cout <V [2] <Endl; // out of range!
- Return 0;
- }
Copy code
Program running result:
22
Just ignore it
131074
In addition, if _ secure_scl_throws is defined as 1, you must call _ crtsetreportmode (_ crt_assert, 0) to disable assert before capturing exceptions in the debug program, and then recover after an exception is captured. This is because the assertion will fail to be triggered before the program throws an exception. The sample code is as follows:
- // Cl/ehs/ d_debug/MDD test2.cpp
- # DEFINE _ secure_scl_throws 1
- # Include <vector>
- # Include <iostream>
- # Include <crtdbg. h> // For _ crtsetreportmode
- Using namespace STD;
- Int main ()
- {
- Vector <int> V;
- V. push_back (11 );
- V. push_back (22 );
- Cout <V [1] <Endl;
- // Disable assert
- Int saved = _ crtsetreportmode (_ crt_assert, 0 );
- Try {
- Cout <V [2] <Endl; // out of range
- }
- Catch (STD: out_of_range &){
- Cout <"out of range" <Endl;
- }
- // Restore assert
- _ Crtsetreportmode (_ crt_assert, saved );
- Return 0;
- }
Copy code
Program running result:
22
Out of range
Part Two Library
Iterator and container
If the macro _ secure_scl is 1, The iterators of all the standard library containers are checked iterator; if the macro _ secure_scl is 0, the iterators of all the standard library containers are unchecked iterator.
The array, pointer, and custom iterator types are uncheck iterator, but they can be adapted to checked iterator through the iterator adapter stdext: checked_array_iterator and stdext: checked_iterator (note: all extension names of C ++ by VC are in the namespace stdext ). For example, the following code snippet uses checked_array_iterator to adapt an array (or pointer) to a checked iterator:
- // Sample code snippet
- Int A [] = {1, 2, 3, 4, 5, 6 };
- Int B [6];
- Copy (A, A + 6, stdext: checked_array_iterator <int *> (B, 5 ));
Copy code
If the macro _ secure_scl is 1, the following container member functions will all perform cross-border checks (checked iterator function ):
Basic_string: operator [], bitset: operator [], deque: Back, deque: Front, deque: operator [], list: Back, list :: front, queue: Back, queue: Front, valarray: operator [], vector: Back, vector: Front, vector: operator [].
Algorithm
VC extends some algorithms in the C ++ standard library. All extension algorithms have a corresponding standard algorithm, which is located in the namespace stdext. the header file is the same as the corresponding standard algorithm, and the parameters and usage are the same, the only difference is that the parameter iterator has different requirements. These extension algorithms have two versions: one checked version and one unchecked version. Their function names are prefixed with checked _ and unchecked _ before the standard algorithm function name _. For example, the standard STD: Copy algorithm has two extension algorithms: stdext: check_copy and stdext: unchecked_copy.
When _ secure_scl is 1,
If the standard algorithm has an extended version, the standard algorithm is equivalent to the checked version of the extended algorithm.
If the output iterator used as an algorithm parameter is a checked iterator, cross-border checks (for example, STD:: Copy, stdext: checked_copy and stdext: unchecked_copy ).
If the output iterator used as an algorithm parameter is an unchecked iterator, a compilation warning (c4996) is generated when the standard algorithm and the checked algorithm are called ); however, no warning message is generated when the unchecked algorithm is called. In this case, no cross-border check is performed.
When _ secure_scl is 0,
If the standard algorithm has an extended version, the standard algorithm is equivalent to the unchecked version of the extension algorithm.
If the output iterator used as an algorithm parameter is a checked iterator, cross-border checks will be performed when standard algorithms, checked extension algorithms, and unchecked extension algorithms are called.
If the output iterator used as an algorithm parameter is an unchecked iterator, a compilation warning (c4996) is generated when the checked algorithm is called ); standard algorithms and unchecked algorithms are called without warning. In this case, no cross-border check is performed.
The extension algorithms of VC are as follows: (Note: Only the checked version is listed here. The unchecked version corresponds to the checked version one by one. You only need to change the function name prefix checked _ to unchecked ):
Delimiter, checked_copy, checked_copy_backward, checked_fill_n, delimiter, checked_merge, delimiter, checked_set_intersection, delimiter, checked_set_union, delimiter, checked_u Ninitialized_fill_n and checked_unique_copy.
Other compilation problems
Please try to compile the following code (note the following compilation options !) :
- // Sample code
- // Cl/ehs/ d_debug/MDD/za test. cpp
- # DEFINE _ secure_scl_throws 1
- # Include <string>
- Int main ()
- {
- Return 0;
- }
Copy code
My vc9 SP1 reports the following error:
Test. cpp
D:/Microsoft Visual Studio 9.0/VC/include/xstring (1566): Error c3861: "_ xran"
: The identifier cannot be found.
D:/Microsoft Visual Studio 9.0/VC/include/xstring (1557): Compile the class template
Member function "char & STD: basic_string <_ ELEM, _ traits, _ ax>: operator [] (unsigned INT )"
Hour
With
[
_ ELEM = char,
_ Traits = STD: char_traits <char>,
_ AX = STD: Allocator <char>
]
D:/Microsoft Visual Studio 9.0/VC/include/xstring (2221): see
Class template instantiation reference of "STD: basic_string <_ ELEM, _ traits, _ ax>"
With
[
_ ELEM = char,
_ Traits = STD: char_traits <char>,
_ AX = STD: Allocator <char>
]
Seeing the source code and error message above, it is estimated that someone will go crazy (I accidentally dug out this compilation scenario and went crazy for a long time !). Indeed, the Code itself does not have any problems (there is no line of its own code at all), it may only be in the compiler or compilation options. As the command line option is provided here, it is easy to find the problem. I met this scenario in an IDE environment, and I spent a lot of effort to find out the culprit from a bunch of options:-P
In fact, this heap of error messages is caused by the compilation option/za. The/za option indicates disabling compiler extension. The checked iterator discussed here is just the compiler extension function, so ...... Over