簡體   English   中英

為什么我出現分段錯誤(核心已轉儲)並且沒有錯誤和警告?

[英]Why do I get Segmentation Fault (core dumped) and no errors and warnings?

我編寫了這段代碼,並在調試了所有似乎已編譯的錯誤和警告之后,但顯示了“分段錯誤(內核已轉儲)”,我不知道為什么要這樣做。

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

typedef struct {
    int nrig, ncol;
    unsigned int **mat;
} matrice, *pmatrice;


void mat_alloca (pmatrice m)
{
    m->mat = malloc(m->nrig * sizeof(*m->mat));
    if (m->mat == NULL) {
        perror(__func__);
        exit(1);
    }
    m->mat[0] = calloc(m->nrig * m->ncol, sizeof(**m->mat));
    if (m->mat[0] == NULL) {
        perror(__func__);
        exit(1);
    }
}

void mat_stampa (pmatrice mat, FILE* f)
{
int i, j;
fprintf(f, "%d %d\n", mat->ncol, mat->nrig);
for (i=0; i<mat->nrig; i++){
    for(j=0; j<mat->ncol; i++){
        fprintf(f, "%2d ", mat->mat[i][j]);
    }
    fprintf(f,"\n");
}
fprintf(f, "\n");
}
void mat_leggi (pmatrice mat, FILE *f)
{
int i, j;
if (fscanf(f, "%d %d", &mat->ncol, &mat->nrig)!=2) {
    fprintf(stderr, "%s:intestazione file non corretta!\n", __func__);
    exit(1);
}
mat_alloca(mat);
for(i=0; i<mat->nrig;i++){
    for(j=0;j<mat->ncol;j++){
        fscanf(f, "%ud ", &mat->mat[i][j]);
    }
}
}
void mat_leggi_da_file(pmatrice mat, char *fn)
{
FILE *f = fopen(fn, "r");
if(!f){
    perror(fn);
    exit(1);
}
mat_leggi(mat,f);
}
void mat_stampa_su_file(pmatrice mat, char *fn)
{
FILE *f = fopen(fn, "w");
if(!f){
    perror(fn);
    exit(1);
}
mat_stampa(mat,f);
}
double media(pmatrice mat)
{
int i,j,somma=0;
int max=0,min=100;
for(i=0;i<mat->nrig;i++){
    for(j=0;j<mat->ncol;j++){
        int val = mat->mat[i][j];
        if(val>max){
            max=val;
        } else if(val<min){
            min=val;
        }
        somma += val;
    }
}
return (somma - max - min)/(double) (mat->nrig * mat->ncol -2);
}
int cmp(int *a1, int *a2, int l)
{
for(;l>0;l--,a1++,a2++){
    if(*a1>*a2) return -1;
    if(*a2>*a1) return 1;
    }
return 0;
}
void mat_bubblesort (pmatrice mat)
{
unsigned int **m = mat->mat;
int i,j,k;
for (j=mat->nrig-1;j>0; j=k) {
    k=-1;
    for(i=0;i<j;i++){
        if(cmp(m[i],m[i+1],mat->ncol)>0) {
            typeof(*m) tmp = m[i];
            m[i]=m[i+1];
            m[i+1]=tmp;
            k=i;
        }
    }
}
}






int main(int argc, char *argv[])
{
matrice mat;
if (argc>1) {
    mat_leggi_da_file(&mat, argv[1]);
    } else {
    mat_leggi_da_file(&mat, "matrice.txt");
}
mat_stampa(&mat,stdout);
printf("\n il valore medio e' %f\n", media(&mat));
mat_bubblesort(&mat);
if (argc>2){
    mat_stampa_su_file(&mat, argv[2]);
} else {
    mat_stampa(&mat, stdout);
}
return 0;
}

第一個問題,您為mat緩沖區分配了一個大緩沖區,但是,您像二維數組(row x col matrix)一樣訪問它。 修正分配嘗試此操作:

void mat_alloca (pmatrice m)
{
    m->mat = malloc(m->nrig * sizeof(*m->mat));
    if (m->mat == NULL) {
        perror(__func__);
        exit(1);
    }
// problem here:    m->mat[0] = calloc(m->nrig * m->ncol, sizeof(**m->mat));    
    int r;
    for (r = 0; r < m->nrig; r++) {
        m->mat[r] = malloc(m->ncol * sizeof(int));
        if (m->mat[r] == NULL) {
            perror(__func__);
            exit(1);
        }
    }
}

另一個問題是mat_stampa函數中的這種錯字,數組索引超出范圍導致了段錯誤:

void mat_stampa (pmatrice mat, FILE* f)
{
    int i, j;
    fprintf(f, "%d %d\n", mat->ncol, mat->nrig);
    for (i=0; i<mat->nrig; i++){

        for(j=0; j<mat->ncol; i++){ // <=== here, should be j++ !!!

            fprintf(f, "%2d ", mat->mat[i][j]);
        }
        fprintf(f,"\n");
    }
    fprintf(f, "\n");
}

“段故障”表示您試圖訪問您無權訪問的內存。

導致分段錯誤的常見錯誤有四個:取消引用NULL,取消引用未初始化的指針,取消引用已釋放(或在C ++中刪除)或超出范圍的指針(對於在函數中聲明的數組) ,並注銷數組的末尾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM