简体   繁体   English

分段故障(核心转储)C

[英]Segmentation fault (core dumped) C

Im having some trouble working with pointers. 我在使用指针时遇到了一些麻烦。 Segmentation fault occours in this part of my code: 我的代码的这一部分出现了分段错误:

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;
    }
}

any ideas on how to resolve this? 关于如何解决这个问题的任何想法? thanks 谢谢

EDIT: main functin here: (i call the function after the fisrt for cycle) 编辑:这里的主要功能:(我在fisrt循环后调用函数)

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 has the wrong type: you should either make it an array of pointers to arrays of int or define it as a 2D array and change the prototypes of the other function to take a variable length 2D array. adjacent_matrix具有错误的类型:您应该使其成为指向int数组的指针数组,或者将其定义为2D数组,并将其他函数的原型更改为采用可变长度的2D数组。

The first approach is simpler and more portable (to pre-c99 compilers): 第一种方法更简单,更便携(对于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;
}

Note: there might be other problems in the code. 注意:代码中可能存在其他问题。

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

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