简体   繁体   English

是什么导致此 function 中的分段错误?

[英]What is causing the segmentation fault in this function?

I am writing a function to search through a 2D array of characters for words from a list.我正在写一个 function 来在一个二维字符数组中搜索列表中的单词。 I am encountering a segmentation fault,probably because of the while loop I have set up according to the backtrace, but I can't see what is causing it.我遇到了分段错误,可能是因为我根据回溯设置了 while 循环,但我看不出是什么原因造成的。

Here is the code for the function that I believe the error is in. Note: This is just the part for the horizontal search.这是我认为错误所在的 function 的代码。注意:这只是水平搜索的部分。

void searchWord(int j, int k, int gridsize, char** arr, char** list, int listPos){
if(j+strlen(*(list+listPos)) <= gridsize){ //Horizontal
    int i = 0;
    while(i < strlen(*(list+listPos)) && *(*(arr+(j+i))+k) == *(*(list+(listPos+i)))){
        i++;
    }
    if(i == strlen(*(list+listPos))){
        while(i > 0 && *(*(arr+(j+i))+k) == *(*(list+(listPos+i)))){
        tolower(*(*(arr+(j+i))+k));
        i--;
        }
        for(i = 0; i < strlen(*(list+listPos)); i++)
            printf("%c", *(*(list+listPos)+i));
    }
}

This is what the backtrace is telling me.这就是回溯告诉我的。

Program received signal SIGSEGV, Segmentation fault.
0x0000000008000fd2 in searchWord (j=0, k=0, gridsize=15, arr=0x8403260, list=0x84032e0, listPos=49)
    at arraysearch.c:110
110                     while(i < strlen(*(list+listPos)) && *(*(arr+(j+i))+k) == *(*(list+(listPos+i)))){
(gdb) bt
#0  0x0000000008000fd2 in searchWord (j=0, k=0, gridsize=15, arr=0x8403260, list=0x84032e0, listPos=49)
    at arraysearch.c:110
#1  0x0000000008000ed4 in searchPuzzle (arr=0x8403260, n=15, list=0x84032e0, listSize=50) at arraysearch.c:96
#2  0x0000000008000d70 in main (argc=2, argv=0x7ffffffee048) at arraysearch.c:68

I fixed the segmentation fault.我修复了分段错误。 I was accessing the word in the list instead of the letter in the words.我正在访问列表中的单词而不是单词中的字母。

*(*(list+(listPos+i)))

Needed to be需要是

*(*(list+listPos)+i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM