Receiving segmentation fault with a program to split sentences

丶灬走出姿态 提交于 2019-12-24 22:30:30

问题


I have problem with pointers when I run my programe I recieve a segmentation falut (core dumped) can anyone help me know which part made this error?

This is a program for splitting sentences by getting a splitter I used two dementioned array

#include <iostream>
using namespace std;


int main(int argc, char **argv)
{
    char Sentence[255];
    cin.getline(Sentence,255);
    char splitter='_';
    int lenOfStr=0;
    int numOfWords=1;
    while (Sentence[lenOfStr]!='\0'){
        if (Sentence[lenOfStr]==splitter) numOfWords++;
        lenOfStr++;
    }

    char** words=new char* [numOfWords];
    int* lenOfEachWord=new int[numOfWords];
    int lenth=0,wordNum=0;
    for (int i=0;i<lenOfStr;i++){
        if (Sentence[i]==splitter) {
            lenOfEachWord[wordNum]=lenth;
            lenth=0;
            wordNum++;
        }
        else lenth++;

    }
    for (int word=0;word<numOfWords;word++){
        int lenth=lenOfEachWord[word];
        words[word]=new char[lenth];
    }
    int word=0;
    int chr=0;
    while (chr<lenOfStr){
        for (int i=0 ;i<lenOfEachWord[word];i++){
            words[word][i]=Sentence[chr];
            chr++;
        }
        word++;
    }
    for (int i=0;i<numOfWords;i++){
        for (int j=0;j<lenOfEachWord[i];j++){
            cout<<words[i][j];
        }
        cout<<endl;
    }
    for (int i=0;i<numOfWords;i++){
        delete[] words[i];
    }
    delete[] words;


    return 0;
}

回答1:


you should try to add log for your code. I try to test with this code, and I find there is some boundary you missed paying attention to ,like you miss to count the last word,and didn't exclude the splitter during the copy process. but I am not sure the way you determine the number of words is whether is right or not, I think the sample input should have only two words.

but I will leave it for you to decide. hope it helps

input : _test_string_

outputs :

numOfWords: 4
lenOfStr: 13
0 4 6 0 
0
1
1 0 1 t
1 1 2 e
1 2 3 s
1 3 4 t
2
2 0 6 s
2 1 7 t
2 2 8 r
2 3 9 i
2 4 10 n
2 5 11 g

test
string

#include <iostream>
#include <cstring>
using namespace std;



int main(int argc, char **argv)
{
    char Sentence[255];
    cin.getline(Sentence,255);
    char splitter='_';
    int lenOfStr=0;
    int numOfWords=1;
    while (Sentence[lenOfStr]!='\0'){
        if (Sentence[lenOfStr]==splitter) numOfWords++;
        lenOfStr++;
    }
    std::cout <<"numOfWords: " <<  numOfWords <<std::endl;
     std::cout <<"lenOfStr: " <<  lenOfStr <<std::endl;
    char** words=new char* [numOfWords];
    int* lenOfEachWord=new int[numOfWords];

    int lenth=0,wordNum=0;
    for (int i=0;i<lenOfStr;i++){
        if (Sentence[i]==splitter) {
            lenOfEachWord[wordNum]=lenth;
            lenth=0;
            wordNum++;
        }
        else lenth++;
    }
    // be care with the boundary 
    if (lenth >= 0)
    {
        lenOfEachWord[wordNum]=lenth;
        wordNum++;
    }
    for(int i =0 ; i< numOfWords; i++)
    {
        std::cout << lenOfEachWord[i] << " ";
    }
    std::cout<<std::endl;
    for (int word=0;word<numOfWords;word++){
        int lenth=lenOfEachWord[word];
        words[word]=new char[lenth];
    }
    int word=0;
    int chr=0;
    while (chr<lenOfStr){
        if (Sentence[chr] == splitter )
        {
            chr++;
            continue;
        }
        std::cout << word << endl;
        for (int i=0 ;i<lenOfEachWord[word];i++){
            words[word][i]=Sentence[chr];
            std::cout << word << " " << i << " " << chr<< " "<< Sentence[chr]<<endl;
            chr++;
        }
        word++;
    }
    for (int i=0;i<numOfWords;i++){
        for (int j=0;j<lenOfEachWord[i];j++){
            cout<<words[i][j];
        }
        cout<<endl;
    }
    for (int i=0;i<numOfWords;i++){
        delete[] words[i];
    }
    delete[] words;


    return 0;
}



回答2:


Here:

while (chr<lenOfStr){
    for (int i=0 ;i<lenOfEachWord[word];i++){
        words[word][i]=Sentence[chr];
        chr++;
    }
    word++;
}

you fail to deal with the splitter character, and the loop fails badly if the number of characters in Sentence is not exactly what is expected.

More generally, you wrote a lot of code before testing any of it, which is a sure path to error.



来源:https://stackoverflow.com/questions/22265730/receiving-segmentation-fault-with-a-program-to-split-sentences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!