strsep segmentation faults on different string pointer/array types

ε祈祈猫儿з 提交于 2019-12-24 12:15:00

问题


Platform: Linux, OSX
Compiler: GCC

I've got a simple program which is currently confounding me - I know that I'm messing with a couple different kinds of arrays/pointers to produce this problem - its intentional - I'm trying to understand it.

The code as listed will compile and run as expected, but changing data4 in the call to strsep(&data4, "e"); to data1 or data3 causes a segmentation fault. I would like to understand why.

#include <stdio.h>
#include <string.h>

int main(int c, char** v) {
    char* data1 = "hello\0";
    char* data2 = strdup(data1);
    size_t sz = strlen(data1);
    char data3[sz+1];
    char* data4;

    memset(data3, 0, sz+1);
    data4 = strncpy(data3, data1, sz);
    data4[sz] = '\0';

    char* found = strsep(&data4, "e");

    if (found == NULL) {
        printf("nothing found\n");
    } else {
        printf("found e\n");
    }

    return 0;
}

回答1:


changing data4 in the call to strsep(&data4, "e"); to data1 or data3 causes a segmentation fault.

In this case:

char* found = strsep(&data1, "e");

the string pointed to by data1 is a literal so it cannot be changed. When strsep() tries to place a '\0' into it, segfault.

in the other case:

char* found = strsep(&data3, "e");

data3 is an array, not a pointer (even though arrays easily evaluate to pointers, they are not actually pointers), so strsep() cannot update the value of the pointer which is what it tries to do once it finds the token. I get the following warning from gcc attempting to point this out:

test.c:17: warning: passing argument 1 of 'strsep' from incompatible pointer type


来源:https://stackoverflow.com/questions/3972332/strsep-segmentation-faults-on-different-string-pointer-array-types

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