尝试访问struct中的指针时出现分段错误

时间:2014-07-27 21:50:29

标签: c pointers struct

我正在尝试访问结构内部的指针,我尝试将*放在结构指针前面 访问结构中的指针,但它是段错误。

*ptr->numberOfClients = clients;

上的代码段错误
int getNumberOfClients(struct fuzzerObj *ptr)
{
    int rtrn;
    long clients;
    char *input;
    char *holder = NULL;

    printf(BOLDBLUE"How many clients will you be connecting to this fuzz server?\n"RESET);
    printf(BOLDBLUE"---> "RESET);
    rtrn = getUserInput(&input);
    if(rtrn < 0)
    {
        errorHandler("Can't Get User input\n", FUNCTION_ID_GET_NUMBER_OF_CLIENTS);
        return -1;
    }
    if (strlen(input))         
    {
         clients = strtol(input, &holder, 10);
         if (input == holder)
         {
             errorHandler("invalid long conversion\n", FUNCTION_ID_GET_NUMBER_OF_CLIENTS);
             return -1;
         }
    }
    else
    {
        errorHandler("No Value To Compute\n", FUNCTION_ID_GET_NUMBER_OF_CLIENTS);
        return -1;
    }
    *ptr->numberOfClients = clients;

    free(input);
    return 0;
}

int getUserInput(char **buf)
{
    int i = 0, max = 1024, c;
    *buf = reallocarray(NULL, 1025, sizeof(char *));
    if(*buf == NULL)
    {
        errorHandler("Mem Error\n", FUNCTION_ID_GET_USER_INPUT);
        free(*buf);
        return -1;
    }
    while (true) { // skip leading whitespace
        c = getchar();
        if (c == EOF) break; // end of file
        if (!isspace(c)) {
            ungetc(c, stdin);
            break;
        }
    }
    while (true) {
        c = getchar();
        if (isspace(c) || c == EOF) // at end, add terminating zero
            buf[i] = 0;
        break;
    }
    *buf[i] = c;
    if (i==max-1) { // buffer full
        max = max+max;
        *buf = (char*)realloc(*buf,max); // get a new and larger buffer
        if (buf == 0)
        {
            errorHandler("Realloc Error\n", FUNCTION_ID_GET_USER_INPUT);
            return -1;
        }
    }
    i++;
    return 0;
}

这是结构

struct fuzzerObj
{
    int parserResponse;
    int *numberOfClients;
    int *clientFuzzerType[1024];
    int *clientSockets[1024];
    int *clientApplication[1024];
    int *clientFuzzer[1024];
    int *connectedClients;
    int *socket;
    int *fuzzer;
    int *application;
    dispatch_queue_t queue;
}; 

2 个答案:

答案 0 :(得分:1)

我发现存在一个主要问题(除非您未在代码段中显示)。 numberOfClients被声明为:

struct fuzzerObj
{
    ...
    int *numberOfClients;
    ...
};

在为其分配int之前。您必须指定内存来存储int

1

    ptr->numberOfClients = malloc(sizeof(*(ptr->numberOfClients)));
    *(ptr->numberOfClients) = clients;
    ...
    free(ptr->numberOfClients);

2

    int temp;
    ptr->numberOfClients = &temp;
    *(ptr->numberOfClients) = clients;
    ...
    // Write to a file here???

另一个问题......为什么fuzzerObj指针的字段?如果你制作int而不是指向int的指针,那么你就不会遇到你遇到的困难。

修改

上面显示的第二种方法并不安全,因为一旦声明temp return s,temp的函数不再存在,因此numberOfClients不会出现这种情况。 t有有效的记忆,不应该使用。

答案 1 :(得分:0)

完全不清楚为什么数据成员numberOfClients定义为指针而不是int类型的对象

int *numberOfClients;

尝试以下

ptr->numberOfClients = ( int * )malloc( sizeof( int ) );
if ( ptr->numberOfClients ) *ptr->numberOfClients = clients;

还要考虑变量客户端定义为

long clients;

在某些实现中,sizeof(lomg)可以大于sizeof(int)。

一般来说这是一个糟糕的设计。目前还不清楚结构的数据成员是否已初始化并且是否已分配相应的内存。