Segmentation fault occurs in the program sometimes, but not always. How do I correct it?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 11:55:21

问题


My program compiles without any error, and it also executes fine for some inputs. But when I give an upper bound input, the .exe file stops working, and no dump file is created.

Please help me out on this one.

#include<stdio.h>
#include<stdlib.h>

#define SIZE 200000

double finval = 32766;
void addf(double st);
void rec(double** tam, long src, long des, double n, double sum);
void op(long u, long v, double** tam, long src, long dest, double n);

int main()
{
    double n, m, q;
    long c, t, i, j, s, d, u, v, w;
    double** mat;
    double** tam;
    double** qe;


    scanf("%lf", &n);
    if(n > 0 || n <= SIZE) {
        mat = (double **) malloc(n * sizeof(int *));
        tam = (double **) malloc(n * sizeof(int *));
        for(i = 0; i < n; i++) {
            mat[i] = (double *) malloc(n * sizeof(int));
            tam[i] = (double *) malloc(n * sizeof(int));
        }
    }
    else
        return 1;

    scanf("%lf", &m);
    if(m <= 0 || m>SIZE)
        return 1;

    for(i = 0; i < n; i++)
        for(j = 0; j < n; j++) {
            mat[i][j] = 0;
            tam[i][j] = 0;
        }

    for(i = 0; i < m; i++) {
        scanf("%ld %ld %ld", &u, &v, &w);
        mat[u][v] = mat[v][u] = w;
    }

    scanf("%ld %ld", &s, &d);

    scanf("%lf", &q);
    if(q > 0 || q <= SIZE) {
        qe = (double **) malloc(q * sizeof(int *));
        for(i = 0; i < q; i++)
            qe[i] = (double *) malloc(2 * sizeof(int));
    }
    else
        return 1;

    for(i = 0; i < q; i++) {
        scanf("%ld %ld", &u, &v);

        qe[i][0] = u;
        qe[i][1] = v;

    }

    for(i = 0; i < q; i++) {
        for(c = 0; c < n; c++)
            for(t = 0; t < n; t++)
                tam[c][t] = mat[c][t];
        finval = 32766;
        op(qe[i][0], qe[i][1], tam, s, d, n);
        printf("\n%g", finval);
    }

    for(i  =  0; i < n; i++) {
        free(mat[i]);
        free(tam[i]);
    }
    free(tam);
    free(mat);
    for(i = 0; i < q; i++)
        free(qe[i]);
    free(qe);
    return 1;
}

void op(long u, long v, double** tam, long src, long dest, double n)
{
    double sum = 0;
    tam[u][v] = tam[v][u] = 0;
    rec(tam, src, dest, n, sum);
};

void rec(double** tam, long src, long des, double n, double sum)
{
    double que[100], dat;
    long front = -1, rear = -1;
    long srec, ref;

    ref = src;
    for(; ref < n; ref++) {
        if(tam[src][ref]! = 0) {
            que[++rear] = ref;
        }
    }

    if(src == des) {
        addf(sum);
        return;
    }
    else
        while(front != rear) {
            srec = que[++front];
            dat = tam[src][srec];
            rec(tam, srec, des, n, sum+dat);
        }

    /*for(i = 0; i < n; i++) {
              for(j = 0;j < n;j++)
                  printf("%lf", tam[i][j]);
              printf("\n");
      }*/

};

void addf(double st)
{
    if(finval > st) {
        finval = st;
    }

};

I use gcc compiler and I have also tried the gdb debugger, but could not solve the problem.


回答1:


(double *) malloc(n * sizeof(int));

This is not right. If you wish to allocate room for n double numbers, use malloc(n * sizeof(double)).




回答2:


when n = SIZE or even near the value of SIZE than you will bloat your memory usage which might case memory overflow and crash your program since you do malloc(n * sizeof(int *)) n times ! so it will be like 200000 memory allocations of size 200000 * sizeof(int) each.




回答3:


Quicksort is notoriously tricky. In your code nothing prevents i and j to wander off past left and right, so they can very well stray out of your array.



来源:https://stackoverflow.com/questions/9907055/segmentation-fault-occurs-in-the-program-sometimes-but-not-always-how-do-i-cor

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