Realloc和SIGABRT

时间:2018-01-13 14:47:13

标签: c realloc

我正在制作一个程序来发现从一个顶点到另一个顶点的最佳路径。好吧,我创建顶点,为邻居列表分配内存。当我在两个顶点之间创建连接时,我重新分配列表的内存以向邻居列表添加一个顶点。它似乎在前两次工作,但在第三次它崩溃并给我信号“SIGABRT”。我找不到任何论坛的问题的解决方案。这是我的代码:

typedef struct vertex{
    char* name;
    Vertex** neighbors;
    float x, y;
}Vertex;

Vertex* create_vertex(char* name, float x, float y){
    Vertex* vertex = (Vertex*) malloc(sizeof(Vertex));
    vertex->x = x;
    vertex->y = y;
    vertex->name = (char*) malloc(sizeof(char)*strlen(name) + 1);
    strcpy(vertex->name, name);
    vertex->neighbors = (Vertex**) malloc(sizeof(Vertex*));
    vertex->neighbors[0] = NULL;

return vertex;
}

void creates_connection(Vertex* v1, Vertex* v2){
    if(areNeighbors(v1, v2)){
        printf("Vertices ja sao vizinhos");
        return;
    }

    Vertex** real;

    if(v1->neighbors[0] == NULL){
        real = (Vertex**) realloc(v1->neighbors, sizeof(Vertex*)+1);

        if(real != NULL){
            v1->neighbors = real;
            v1->neighbors[0] = v2;
            v1->neighbors[1] = NULL;
        }else printf("Nao foi possivel realocar memoria");

    }else{
        Vertex* aux = v1->neighbors[0];
        int i = 0;

        while(aux != NULL){
            aux = v1->neighbors[i];
            i++;
        }

        real= (Vertex**) realloc(v1->neighbors, (sizeof(Vertex*))*(i+1));

        if(real!= NULL){
            v1->neighbors= real;
            v1->neighbors[i] = v2;
            v1->neighbors[i+1] = NULL;
        }else printf("Nao foi possivel realocar memoria");
    }

    if(v2->neighbors[0] == NULL){
        real = (Vertex**) realloc(v2->neighbors, sizeof(Vertex*));

        if(real != NULL){
            v2->neighbors = realocado;
            v2->neighbors[0] = v1;
            v2->neighbors[1] = NULL;
        }

    }else{
        Vertex* aux = v2->neighbors[0];
        int i = 0;

        while(aux != NULL){
            aux = v2->neighbors[i];
            i++;
        }

        //HERE, BELLOW, IT CRASHES, DOESN'T PASS TO THE IF
        real = (Vertex**) realloc(v2->neighbors, (sizeof(Vertex*))*(i+1));

        if(real != NULL){
            v2->neighbors = real;
            v2->neighbors[i] = v1;
            v2->neighbors[i+1] = NULL;
        }else printf("Nao foi possivel realcoar memoria");

    }
}

我不知道这是否是最好的方式来做我正在做的事情,我接受建议,但我也想了解发生了什么,我的意思是,为什么这个SIGABRT正在发生。

2 个答案:

答案 0 :(得分:0)

这个答案专注于指出(部分)发布代码中的问题。最后,这个答案包含了一个代码更改的建议,该代码更改将适用于您当前的设计。如果您愿意进行重新设计,请查看@wildplasser

的答案

这里有一个问题

if(v1->neighbors[0] == NULL){
    real = (Vertex**) realloc(v1->neighbors, sizeof(Vertex*)+1);
                                                           ^^^^
                                               This only increase with 1 byte
                                               but you want 1 pointer

在这里

if(v2->neighbors[0] == NULL){
    real = (Vertex**) realloc(v2->neighbors, sizeof(Vertex*));
                                                        ^^^^
                                               No increase at all

此处还有一个问题:

    Vertex* aux = v1->neighbors[0];
    int i = 0;

    while(aux != NULL){
        aux = v1->neighbors[i];
        i++;
    }

    real= (Vertex**) realloc(v1->neighbors, (sizeof(Vertex*))*(i+1));

由于你的设计要求数组的末尾总是有一个NULL指针,你需要让i从1开始,即

    int i = 1;

当你写出一边分配的内存时,这部分也有问题 - 记住索引从零开始:

    real= (Vertex**) realloc(v1->neighbors, (sizeof(Vertex*))*(i+1));

    if(real!= NULL){
        v1->neighbors= real;
        v1->neighbors[i] = v2;
        v1->neighbors[i+1] = NULL;  // UPS... write outside allocated memory
                                    // Writing to [i+1] requires that you
                                    // have i+2 pointers but you only
                                    // have i+1

有趣的是,当第一个指针为NULL时,您不需要处理这两个特殊情况。 else中的代码也可以处理第一个指针为NULL的情况。

所以这段代码:

if(v1->neighbors[0] == NULL){
    real = (Vertex**) realloc(v1->neighbors, sizeof(Vertex*)+1);

    if(real != NULL){
        v1->neighbors = real;
        v1->neighbors[0] = v2;
        v1->neighbors[1] = NULL;
    }else printf("Nao foi possivel realocar memoria");

}else{
    Vertex* aux = v1->neighbors[0];
    int i = 0;

    while(aux != NULL){
        aux = v1->neighbors[i];
        i++;
    }

    real= (Vertex**) realloc(v1->neighbors, (sizeof(Vertex*))*(i+1));

    if(real!= NULL){
        v1->neighbors= real;
        v1->neighbors[i] = v2;
        v1->neighbors[i+1] = NULL;
    }else printf("Nao foi possivel realocar memoria");
}

可以简化为

    Vertex* aux = v1->neighbors[0];
    int i = 1;         // NOTICE: Use 1 instead of 0

    while(aux != NULL){
        aux = v1->neighbors[i];
        i++;
    }

    real= (Vertex**) realloc(v1->neighbors, (sizeof(Vertex*))*(i+1));

    if(real!= NULL){
        v1->neighbors= real;
        v1->neighbors[i-1] = v2;   // NOTICE: i-1
        v1->neighbors[i] = NULL;   // NOTICE: i
    }else printf("Nao foi possivel realocar memoria");

这也适用于您处理v2

的第二个区块

答案 1 :(得分:0)

  • 而不是指针数组末尾的NULL sentinel,你可以跟踪(使用过的)大小
  • 不要在每次添加时重新分配,使用更大的增量
  • 如果第一个参数为NULL,则realloc()的工作方式与malloc()
  • 类似
  • 集中你的(重新)分配,这将集中你的错误
  • 将内联静态函数(在某些优化级别)
filterNot()