简体   繁体   English

访问字符串数组时出现奇怪的分段错误

[英]Weird segmentation fault while accessing string array

Sorry to post my project code directly. 抱歉直接发布我的项目代码。 I've been trying to wrap my head around this weird seg fault which occurs at the line for(j=0; j<100 && *nnames[j] != (char *) NULL; j++) . 我一直在努力解决这个奇怪的段错误,该错误发生在for(j = 0; j <100 && * nnames [j]!=(char *)NULL; j ++)行中 Isn't it legal to access a char** array this (*arr[]) way ? 以这种方式(* arr [])访问char **数组不合法吗?

    char** nnames = getcannames();
    char * new_str ;

    int j =0, len=0;
    ////////////////SEG FAULT HERE //////////////////
    for(j=0; j<100 && *nnames[j] != (char *) NULL; j++){
        len = len + strlen(nnames[j]);

    }
    if((new_str = (char*) malloc(len + 3)) != NULL){
        new_str[0] = '\0';   // ensures the memory is an empty string
        int i=0;    
        //setbuf(client_reply, NULL);
        for(i=0; i<7; i++){ //fix this, garbage values after index 68
            if(*nnames[i] == (char *) NULL) break;

            char *canname = strsave(nnames[i]);


            if( (find_newline = strchr( canname, NEWLINE )) != NULL )
                *find_newline = EOS;
            if(strcmp(canname, "!") != 0){
                strcat(new_str, canname);
                strcat(new_str, "\n");
            }

            //strcat(new_str, "\n\n");  
        }
        strcat(new_str,"\n\0");
        printf("%s", new_str);
        //strcpy( new_str, buf );
        buf = new_str;

    } else {
        perror("malloc failed!\n");
        // exit?
    }


char** getcannames(){
    //INITIALIZE
     char *names[100];
    int i;
    for(i=0; i<100; i++){
        names[i] = strsave("\0");
    }
    int namespos = 0;

     struct sym_list *sp;
     for( sp = Head.s_next;
     sp != (struct sym_list *) NULL;
     sp = sp->s_next )
    {
    if(getcannameindex(names, sp->s_sym.v_value) == -1){
        //strcpy(names[namespos++], sp->s_sym.v_name);
        names[namespos++] = strsave(sp->s_sym.v_value);
    }
    }
    return names;

}

If nnames is a pointer to the first element of an array of pointers of type char * then the valid code will look like 如果nnames是指向char *类型的指针数组的第一个元素的指针,则有效代码如下所示

for ( j = 0; j < 100 && nnames[j] != NULL; j++ ){
        len = len + strlen(nnames[j]);

provided that the last element of the array is a null pointer. 假设数组的最后一个元素为空指针。

The same is valid for statement 声明同样有效

if(*nnames[i] == (char *) NULL) break;

that is it has to be rewritten like 那就是它必须像

if ( nnames[i] == NULL ) break;

Also this function 还有这个功能

char** getcannames(){
    //INITIALIZE
     char *names[100];

     //...

     return names;

}

has undefined behaviour because it returns pointer to the first element of a local array that will be destroyed after exiting the function. 具有未定义的行为,因为它返回指向本地数组第一个元素的指针,该指针在退出函数后将被破坏。

Take into account that if function strsave creates dynamically a copy of the string passed to it as the argument 考虑到如果函数strsave动态创建作为参数传递给它的字符串的副本

char *canname = strsave(nnames[i]);

then the program has memory leaks because you do not free canname. 则该程序有内存泄漏,因为您没有释放canname。

And of course you may write like 当然,您可能会这样写

strcat(new_str,"\n\0");

or even like 甚至喜欢

strcat(new_str,"\n\0\0\0\0");

but the both statements are equivalent to 但是两个语句都等效于

strcat(new_str,"\n");

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

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