c - 段错误(核心已转储)C

标签 c algorithm segmentation-fault

我在使用指针时遇到了一些问题。我的这部分代码出现段错误:

int **adjacent_matrix[N][N];

void create_graph(int N, int L, int **adjacent_matrix, int *queue, int *integers, int *topological_sort){
    int count, maximum_edges, origin_vertex, destination_vertex;
    total_vertices = N;
    maximum_edges = L;
    int i =0;

    for(count = 1; count <= maximum_edges; count++){
        origin_vertex = integers[i]-1;
        destination_vertex = integers[i+1]-1;
        if(origin_vertex > total_vertices || destination_vertex > total_vertices || origin_vertex < 0 || destination_vertex < 0){
            printf("Edge Co-ordinates are Invalid\n");
            count--;
        }
        else{
            //ERROR HERE !
            adjacent_matrix[origin_vertex][destination_vertex] = 1;
        }

        if(count == maximum_edges){
            break;
        }
        i+=2;
    }
}

关于如何解决这个问题的任何想法?谢谢

编辑:主要功能在这里: (我在第一个 for 循环之后调用该函数)

int main(){

    int i, N, L;

    if(scanf("%d", &N)){};
    if(scanf("%d", &L)){};

    int **adjacent_matrix[N][N];
    int queue[N];
    int integers[N];
    int topological_sort[N];

    for(i=0; i<(L*2); i++){
        if(scanf("%d", &integers[i])){};
    }

    int vertex, count, indegree[N];
    create_graph(N, L, **adjacent_matrix, queue, integers, topological_sort);

    for(i = 0; i < total_vertices; i++){
        indegree[i] = find_indegree_of_vertex(i, **adjacent_matrix, queue, integers, topological_sort);                       
        if(indegree[i] == 0){
            add(N, i, **adjacent_matrix, queue, integers, topological_sort);
        }
    }
    count = 0;
    while(!isEmpty(**adjacent_matrix, queue, integers, topological_sort) && count < total_vertices){
        vertex = del(**adjacent_matrix, queue, integers, topological_sort);
        topological_sort[++count] = vertex;
        for(i = 0; i < total_vertices; i++){
            if(**adjacent_matrix[vertex][i] == 1){
                /*adjacent_matrix[vertex][i] = 0;*/
                indegree[i] = indegree[i] - 1;
                if(indegree[i] == 0){
                    add(N, i, **adjacent_matrix, queue, integers, topological_sort);
                }
            }
        }
    }
    if(count < total_vertices){
        printf("Incoerente\n");
        return EXIT_SUCCESS;
    }
    if(L < (N-1) || check_topological(N, **adjacent_matrix, queue, integers, topological_sort)==0){
        printf("Insuficiente\n");
        return EXIT_SUCCESS;
    }

    for(i = 1; i < count; i++){
        printf("%d", topological_sort[i]+1);
        printf(" ");
    }
    printf("%d\n", topological_sort[count]+1);

    return EXIT_SUCCESS;
}

最佳答案

adjacent_matrix 的类型错误:您应该将其设为指向 int 数组的指针数组,或者将其定义为二维数组并更改另一个数组的原型(prototype)获取可变长度二维数组的函数。

第一种方法更简单且更可移植(对于 c99 之前的编译器):

int main(void) {
    int i, N, L;

    if (scanf("%d", &N) != 1) { exit(1); }
    if (scanf("%d", &L) != 1) { exit(1); }

    int *adjacent_matrix[N];
    int queue[N];
    int integers[N];
    int topological_sort[N];

    for (i = 0; i < N; i++) {
        adjacent_matrix[i] = calloc(sizeof(*adjacent_matrix[i]), N);
    }

    for (i = 0; i < (L * 2); i++) {
        if (scanf("%d", &integers[i]) != 1) { exit(2); }
    }

    int vertex, count, indegree[N];
    create_graph(N, L, adjacent_matrix, queue, integers, topological_sort);

    for (i = 0; i < total_vertices; i++) {
        indegree[i] = find_indegree_of_vertex(i, adjacent_matrix, queue,
                                              integers, topological_sort);                       
        if (indegree[i] == 0) {
            add(N, i, adjacent_matrix, queue, integers, topological_sort);
        }
    }
    count = 0;
    while (!isEmpty(adjacent_matrix, queue, integers, topological_sort)
        && count < total_vertices) {
        vertex = del(adjacent_matrix, queue, integers, topological_sort);
        topological_sort[++count] = vertex;
        for (i = 0; i < total_vertices; i++) {
            if (adjacent_matrix[vertex][i] == 1) {
                adjacent_matrix[vertex][i] = 0;
                indegree[i] = indegree[i] - 1;
                if (indegree[i] == 0) {
                    add(N, i, adjacent_matrix, queue, integers, topological_sort);
                }
            }
        }
    }
    if (count < total_vertices) {
        printf("Incoerente\n");
        return EXIT_SUCCESS;
    }
    if (L < (N - 1) || check_topological(N, adjacent_matrix, queue, integers, topological_sort) == 0) {
        printf("Insuficiente\n");
        return EXIT_SUCCESS;
    }

    for (i = 1; i < count; i++) {
        printf("%d", topological_sort[i] + 1);
        printf(" ");
    }
    printf("%d\n", topological_sort[count] + 1);

    return EXIT_SUCCESS;
}

注意:代码中可能还有其他问题。

关于c - 段错误(核心已转储)C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43005308/

相关文章:

c - 与 SSE 的并行前缀(累积)总和

c - 关于同步对象的一些问题[Linux os]

algorithm - 修正的最短路径算法——顶点有点

java - 给定字典,对字符列表进行排序

algorithm - 最大产品切割算法

c - 函数名称前的星号是什么意思?

C - 数组。仅更改特定值

c++ - 当我写入超出数组末尾时,为什么不会出现段错误?

python - 从 Python 传递字符串时 PyArg_ParseTuple 的段错误

c++ - 似乎从复制构造函数中得到段错误?