Checked iterator of VC

Source: Internet
Author: User
Tags traits

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.

  1. # 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:

  1. // Cl/ehstest1.cpp
  2. # Include <vector>
  3. # Include <iostream>
  4. Using namespace STD;
  5. Void myinvalidparameterhandler (const wchar_t * expression,
  6. Const wchar_t * function,
  7. Const wchar_t * file,
  8. Unsigned int line,
  9. Uintptr_t preserved)
  10. {
  11. Cout <"Just ignore it" <Endl;
  12. }
  13. Int main ()
  14. {
  15. Vector <int> V;
  16. V. push_back (11 );
  17. V. push_back (22 );
  18. Cout <V [1] <Endl;
  19. _ Set_invalid _{
    Tagshow (Event)
    } "> Parameter_handler (myinvalidparameterhandler );
  20. Cout <V [2] <Endl; // out of range!
  21. Return 0;
  22. }

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:

  1. // Cl/ehs/ d_debug/MDD test2.cpp
  2. # DEFINE _ secure_scl_throws 1
  3. # Include <vector>
  4. # Include <iostream>
  5. # Include <crtdbg. h> // For _ crtsetreportmode
  6. Using namespace STD;
  7. Int main ()
  8. {
  9. Vector <int> V;
  10. V. push_back (11 );
  11. V. push_back (22 );
  12. Cout <V [1] <Endl;
  13. // Disable assert
  14. Int saved = _ crtsetreportmode (_ crt_assert, 0 );
  15. Try {
  16. Cout <V [2] <Endl; // out of range
  17. }
  18. Catch (STD: out_of_range &){
  19. Cout <"out of range" <Endl;
  20. }
  21. // Restore assert
  22. _ Crtsetreportmode (_ crt_assert, saved );
  23. Return 0;
  24. }

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:

  1. // Sample code snippet
  2. Int A [] = {1, 2, 3, 4, 5, 6 };
  3. Int B [6];
  4. 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 !) :

  1. // Sample code
  2. // Cl/ehs/ d_debug/MDD/za test. cpp
  3. # DEFINE _ secure_scl_throws 1
  4. # Include <string>
  5. Int main ()
  6. {
  7. Return 0;
  8. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.