C ++ programming debugging tips ---- Reading Notes (3)

Source: Internet
Author: User

Iii. Index out of bounds 1. Dynamic Arrays can be created dynamically using new or vector, but when the index of the current subject exceeds the size, an uncertain error occurs when the new array is modified. vector provides an at (index) function, he throws an out_of_range exception and executes the boundary detection test code (vs2012 + win7 environment): [cpp] # include "stdafx. h "# include" scpp_assert.h "# include" iostream "# include" vector "# define SIZE 10 int _ tmain (int argc, _ TCHAR * argv []) {char * str = new char [SIZE]; str [SIZE + 1] = 'X'; std: vector <char> arrayStr; arrayStr. resize (SIZE); array Str [11] = 'X'; return 0;} The problem is that if we want to perform this security check, we must strictly use at () in every place that accesses the array element () function. Obviously, this approach will reduce the code efficiency, so after testing, we may want a faster [] operator to replace it with each other. However, this replacement greatly modifies the code. Therefore, you can use the following method (to solve this problem, you do not need to use the at function ): [cpp] # ifndef _ SCPP_VECTOR_H _ # define _ SCPP_VECTOR_H _ # include "scpp_assert.h" # include "vector" namespace scpp {template <typename T> class vector: public std: vector <T> {public: typedef unsigned size_type; public: explicit vector (size_type n = 0): std: vector <T> (n) {} vector (size_type n, const T & value): std: vector <T> (n, value) {} template <class InputIterator> Vector (InputIterator first, InputIterator last): std: vector <T> (first, last) {} T & operator [] (size_type index) {SCPP_TEST_ASSERT (index <std :: vector <T>: size (), "Index" <index <"must be less than" <std: vector <T>: size ()); return std: vector <T >:: operator [] (index);} const T & operator [] (size_type index) const {SCPP_TEST_ASSERT (index <std :: vector <T>: size (), "Index" <index <"mu St be less than "<std: vector <T >:: size (); return std: vector <T> :: operator [] (index) ;};}; // namespace scpp template <typename T> inline std: ostream & operator <(std: ostream & OS, const scpp: vector <T> & v) {for (unsigned index = 0; index <v. size (); ++ index) {OS <v [index]; if (index + 1 <v. size () {OS <";}} return OS ;}# endif test code (vs2012 + win7 environment): [cpp] # include" stdafx. h "# inc Lude "scpp_assert.h" # include "iostream" # include "scpp_vector.h" # define SIZE 10 int _ tmain (int argc, _ TCHAR * argv []) {scpp :: vector <char> str (SIZE, 'z'); std: cout <str <std: endl; str [20] = 'X'; return 0 ;} in stdafx. turn on the debugging switch in h: [cpp] # pragma once # include "targetver. h "# include <stdio. h> # include <tchar. h> // TODO: reference other header files required by the program here/* # define SCPP_THROW_EXCEPTION_ON_BUG */# define SCPP_TEST_ASSERT _ ON 2. Static array: scpp: vector vect (SIZE). Its effect is the same as static array, but the problem lies in efficiency. Static arrays are allocated memory on the stack, while the vector Template is allocated using the new operator in the constructor, which is slow. Therefore, the array template: scpp_array.h: [cpp] # ifndef _ SCPP_ARRAY_H _ # define _ SCPP_ARRAY_H _ # include "scpp_assert.h" namespace scpp {template <typename T, unsigned N> class array {public: typedef unsigned size_type; public: array () {} explicit array (const T & initialValue) {for (size_type index = 0; index <N; ++ index) {data _ [index] = initialValue;} size _ Type size () const {return N;} T & operator [] (size_type index) {SCPP_TEST_ASSERT (index <N, "Index" <index <"must be less than" <N); return data _ [index];} const T & operator [] (size_type index) const {SCPP_TEST_ASSERT (index <N, "Index" <index <"must be less than" <N); return data _ [index];} T * begin () {return & data _ [0];} const T * begin () const {return & data _ [0];} T * end (){ Return & data _ [N];} const T * end () const {return & data _ [N];} private: T data _ [N] ;}} template <typename T, unsigned N> inline std: ostream & operator <(std: ostream & OS, const scpp: array <T, N> & v) {for (unsigned index = 0; index <v. size (); ++ index) {OS <v [index]; if (index + 1 <v. size () {OS <";}} return OS ;}# endif // _ SCPP_ARRAY_H _ test code (vs2012 + win7 environment ): [cpp] # include "stdaf X. h "# include" scpp_assert.h "# include" iostream "# include" scpp_vector.h "# include" scpp_array.h "# include" algorithm "# define SIZE 10 int _ tmain (int argc, _ TCHAR * argv []) {scpp: array <int, SIZE> str (1); str [0] = 7; str [1] = 2; str [2] = 8; str [3] = 4; std: cout <str <std: endl; std: sort (str. begin (), str. end (); std: cout <str <std: endl; return 0;} the behavior of this array is exactly the same as that of the static array of C. However, when the SCPP_TEST_ASSERT_ON macro indicating security check is activated during compilation, it will provide the index boundary check. The begin and end methods are provided, so you can use some algorithms of algorithm. 3. Multi-dimensional arrays are actually multi-dimensional arrays, that is, matrices. If the matrix size is known during compilation, it can be easily implemented as an array. Therefore, focus on this situation when the size of the matrix is calculated at runtime. A vector can be used internally for storage. To return the data, you only need to add the index to the row scpp_matrix.h: [cpp] # ifndef _ SCCP_MATRIX_H _ # define _ SCCP_MATRIX_H _ # include "ostream" # include "vector" # include "scpp_assert.h" namespace scpp {template <typename T> class matrix {public: typedef unsigned size_type; public: matrix (size_type numRows, size_type numCols): rows _ (numRows), cols _ (numCols), data _ (numCols * numRows) {SCPP_TEST_ASSERT (numRows> 0, "Number of rows in a matrix must be positive"); SCPP_TEST_ASSERT (numCols> 0, "Number of cols in a matrix must be positive ");} matrix (size_type numRows, size_type numCols, const T & initValue): rows _ (numRows), cols _ (numCols), data _ (numCols * numRows) {counts (numRows> 0, "Number of rows in a matrix must be positive"); SCPP_TEST_ASSERT (numCols> 0, "Number of cols in a matrix must be positive");} size_type numRows () const {return rows _;} size_type numCols () const {return cols _;} T & operator () (size_type row, size_type col) {return data _ [index (row, col)];} const T & operator () (size_type row, size_type col) const {return data _ [index (row, col)];} private: size_type index (size_type row, size_type col) const {SCPP_TEST_ASSERT (row <rows _, "Row" <row <"must be less than" <rows _); SCPP_TEST_ASSERT (col <cols _, "Col" <col <"must be less than" <cols _); return cols _ * row + col;} private: size_type rows _; size_type cols _; vector <T> data _ ;};}template <typename T> inline std: ostream & operator <(std: ostream & OS, const scpp: matrix <T> & m) {for (unsigned rowIndex = 0; rowIndex <m. numRows (); ++ rowIndex) {for (unsigned colIndex = 0; colIndex <m. numCols (); ++ colIndex) {OS <m (rowIndex, colIndex); if (colIndex <m. numCols () {OS <"\ t" ;}} return OS ;}# endif test code (vs2012 + win7 environment): [cpp] # include "stdafx. h "# include" scpp_assert.h "# include" iostream "# include" scpp_vector.h "# include" scpp_array.h "# include" scpp_matrix.h "# include" algorithm "# define SIZE 10 int _ tmain (int argc, _ TCHAR * argv []) {scpp: matrix <int> scppMatrix (2, 2); scppMatrix (0, 0) = 1; scppMatrix (0, 1) = 2; scppMatrix (1, 0) = 3; scppMatrix (1, 1) = 4; scppMatrix (6, 6) = 4; std: cout <scppMatrix <std :: endl; return 0 ;}

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.