A hidden array of access to the problem (unexpectedly tangled half an hour, it may be my level is too low)

Source: Internet
Author: User

The code is simplified as follows:

#include <iostream>

#include <string>

using namespace Std;

int main ()


String str= "Hello world!";

int len = Str.length ();//Get length

for (int i=0;str[i]!= ' && (I<len); ++i);//Iterate through every variable in a string knowing that a space is found is always an error when debugging in this row because array access is out of bounds



The red line does go out of bounds because the str[i]!= ', which is executed first, is bound to be out of bounds every time the criteria for a for-in decision is executed.

You can change the order for (int i=0; (i<len) &&str[i]!= "; ++i) if you want not to cross the border.

Did not expect such a small problem, tangled for half an hour, so write down, deepen the impression, in order to develop good code habits.

A hidden array of access to the problem (unexpectedly tangled half an hour, it may be my level is too low)

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.