Problem with EXC_BAD_ACCESS

Hi,I got a hard time dealing with the exc_bad_access problem. Can anyone figure out where I've done wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void write_to_array(string s,int a[][NUM],int i){
    vector<string> vec;
    int position=s.find(":");
    string temp;
    for (int p=position+1; p<s.size(); p++) {
        if(s[p]==' '){
            temp.clear();
            int f=1;
            while (s[p+f]!=' ') { //this line :Thread 1 EXC_BAD_ACCESS(code=1,address=0x100200000)
                f++;
                
            }
            temp=s.substr(p+2,f);
            vec.push_back(temp);
        }
        
    }
    
    for (int m=0; m<200 ; m++) {
        a[i][m]=atoi(vec[m].c_str());  //EXC_BAD_ACCESS(code=2)
    }
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void write_to_array(string s,int a[][NUM],int i){
    vector<string> vec;
    int position=s.find(":"); //no checking for success
    string temp;
    for (int p=position+1; p<s.size(); p++) {
        if(s[p]==' '){
            temp.clear();
            int f=1;
            while (s[p+f]!=' ') { //possible out of bounds
                f++;
                
            }
            temp=s.substr(p+2,f); //nonsense.
            vec.push_back(temp);
        }
        
    }
    
    for (int m=0; m<200 ; m++) {
        a[i][m]=atoi(vec[m].c_str());  //¿how do you know the size of the vector? Also, ¿why don't just pass the row to work with?
    }
}
About the nonsense part.
`s[p]' has an space, you don't know anything about `s[p+2]', which may be out of bounds.
`f' seems to be counting the number of spaces, but it is used as the number of characters to extract.
`p' just increases in 1, giving overlaping substrings.

All this for ending converting an string to a number, don't understand why don't you read the numbers in the first place.


Please state the purpose of the function.
Topic archived. No new replies allowed.