Rokusjar
Rokusjar

Reputation: 209

Program stops working when function is called.

There is a problem somewhere in this function, unfortunately compiler doesnt says where. Program just stops working and it has to be shut down.

I am a begginer in c++ so I maz have done something stupid but i dont know what.

I will apreciate any help.

vector< vector<string> >gsiread16::make_section(vector<string> blocks) {

string code;
string code2;
vector<string> section;
vector< vector<string> > sections;

for ( int i = 0; i < blocks.size(); i++) {

    code = blocks[ i ].substr(0,3);

    if( code == "*41" ) {

                code2 = blocks[  i+1 ].substr(0,3);

                if( code2 != "*11" ) continue;

                int index = i +1;

                while(code2 == "*11" ) {

                    section.push_back( blocks[ index ] );
                    index++;

                }

                sections.push_back(section);
                section.clear();
                i = index - 1;

    } else continue;

}

return sections;

}

Upvotes: 0

Views: 121

Answers (3)

swaps
swaps

Reputation: 596

Out of bounds reached when i=blocks.size()-1 for : code2 = blocks[ i+1 ].substr(0,3);

Also this is an endless loop : while(code2 == "*11" ) {

                section.push_back( blocks[ index ] );
                index++; }

Upvotes: 0

Zaiborg
Zaiborg

Reputation: 2522

> while(code2 == "*11" ) {
>     section.push_back( blocks[ index ] );
>     index++; }

that can never be passed ...

i = index - 1;

could end in endless loop

Upvotes: 3

masoud
masoud

Reputation: 56479

Out of bound indexing where 0 <= i < size:

 blocks [i+1]

i+1 exceeds the valid index where i=blocks.size()-1.

Moreover, this loop never finishes:

while(code2 == "*11" ) {
   section.push_back( blocks[ index ] );
   index++;
}

Upvotes: 2

Related Questions