Vector subscript out of range

Source: Internet
Author: User

At the beginning, I wrote logs to record what I did. I didn't expect others to check the logs. So there is no explanation, but something is written randomly.


In fact, the problem is very simple. The question has already explained everything. The error is an out-of-bounds. Check the code carefully to find out where it may cross-border.

When I ran the program yesterday, I suddenly encountered an error as described in the question. The error is obvious. The subscript of the vector is out of bounds, but there has been no problem before. So I carefully read the code, debug, and then took out the program that could run the day before. This error still occurs.

At the beginning, I was unwilling to start thinking that I could run before yesterday. The error should be in the code of yesterday.

So I finally couldn't find it after searching for a day.

Continue searching today, but today I am not looking for the code I wrote yesterday. I started to look at the previously called functions and found out the results in a very short time, a very low-level hand error...

 

Conclusion: I cannot trust myself too much and believe in these "standards". After all, it is summed up by so many people who have been used by more people. If this error is prompted, I will first look for this error.

Be more careful

You must learn to debug and run the code several times more.

 

In addition, I understand at & [] in the vector. The source code is as follows:


Const_reference at (size_type _ POS) const <br/> {// subscript nonmutable sequence with checking <br/> If (SIZE () <= _ POS) <br/> _ xran (); <br/> return (* (begin () + _ POS )); <br/>}< br/> reference at (size_type _ POS) <br/> {// subscript mutable sequence with checking <br/> If (SIZE () <= _ POS) <br/> _ xran (); <br/> return (* (begin () + _ POS )); <br/>}< br/> const_reference operator [] (size_type _ POS) const <br/> {// subscript nonmutable sequence <br/> # If _ has_iterator_debugging <br/> If (SIZE () <= _ POS) <br/>{< br/> _ debug_error ("vector subscript out of range"); <br/> _ scl_secure_out_of_range; <br/>}< br/> # endif/* _ has_iterator_debugging */<br/> _ scl_secure_validate_range (_ POS <size ()); <br/> return (* (_ myfirst + _ POS); <br/>}< br/> reference operator [] (size_type _ POS) <br/> {// subscript mutable sequence <br/> # If _ has_iterator_debugging <br/> If (SIZE () <= _ POS) <br/>{< br/> _ debug_error ("vector subscript out of range"); <br/> _ scl_secure_out_of_range; <br/>}< br/> # endif/* _ has_iterator_debugging */<br/> _ scl_secure_validate_range (_ POS <size ()); <br/> return (* (_ myfirst + _ POS); <br/>} 

The difference between this member function and member operator Function OPERATOR []Is that Vector:Signals if the requested position is out of range by throwing Out_of_rangeException. OPERATOR [] signals only if _ has_iterator_debugging. although it is not too clear, it should be that at () will check for out-of-bounds, and [] will not check out-of-bounds except for such debug. So I will use. At () later. However, this will reduce the efficiency, or [] It should be better in practice. First, it is easy to read, second, it only checks out-of-bounds during debugging, and does not check when debugging is complete, saving time. However, it seems that C ++ 0x will check both of them. Not clear. Wait for the new standard

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.