[英]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.