简体   繁体   English

在C中将字符串文字作为参数传递时的分段错误

[英]Segmentation Fault in Passing String Literal as Argument in C

I was trying to write a program to perform matrix multiplication using OpenMP in C. I am facing a segmentation fault error whenever I am trying to pass a string literal as an argument to a function. 我试图编写一个使用C语言中的OpenMP执行矩阵乘法的程序。每当我尝试将字符串文字作为参数传递给函数时,我都会遇到分段错误。

#include<stdio.h>
#include<time.h>
#include<omp.h>

struct matrix{
    int r;
    int c;
    int mat[1000][1000];
};

void read_matrix(char* fname, struct matrix* m){
    FILE *fp;
    fp = fopen(fname,"r");

    fscanf(fp, "%d %d",&m->r,&m->c);
    for(int i=0;i<m->r;i++){
        for(int j=0;j<m->c;j++){
            fscanf(fp, "%d", &m->mat[i][j]);
        }
    }
    fclose(fp);
}

void write_matrix(char* fname, struct matrix m){
    FILE *fp;
    fp = fopen(fname,"w");
    fprintf(fp,"%d %d\n",m.r,m.c);
    for(int i=0;i<m.r;i++){
        for(int j=0;j<m.c;j++){
            fprintf(fp,"%d\n",m.mat[i][j]);
        }
    }
    fclose(fp);
}

void main(){
    struct matrix m1;
    struct matrix m2;
    struct matrix res;
    read_matrix("m1",&m1);
    read_matrix("m2",&m2);
    int r1 = m1.r;
    int c1 = m1.c;
    int c2 = m2.c;
    res.r = r1;
    res.c = c2;
    for(int i=0;i<r1;i++){
        for(int j=0;j<c2;j++){
            res.mat[i][j] = 0;
        }
    }
    #pragma omp parallel
    {
        #pragma omp for
        for(int i = 0; i < r1; i++){
            for(int j = 0; j < c2; j++){
                for(int k = 0; k < c1; k++){
                    #pragma omp atomic update
                    res.mat[i][j] += m1.mat[i][k]*m2.mat[k][j];
                }
            }
        }
    }
    write_matrix("res",res);

}

The code shows Segmentation fault (core dumped) when run. 该代码在运行时显示Segmentation fault (core dumped) When run on GDB, it shows that Program received signal SIGSEGV, Segmentation fault. 0x0000000000400ad9 in main () at mm.c:40 40 read_matrix("m1",&m1); 在GDB上运行时,表明Program received signal SIGSEGV, Segmentation fault. 0x0000000000400ad9 in main () at mm.c:40 40 read_matrix("m1",&m1); Program received signal SIGSEGV, Segmentation fault. 0x0000000000400ad9 in main () at mm.c:40 40 read_matrix("m1",&m1); Before the first read_matrix() call I added a printf statement printf("check\\n"); 在第一次read_matrix()调用之前,我添加了一条printf语句printf("check\\n"); The printf call now started throwing a segmentation fault. 现在,printf调用开始引发分段错误。 I am assuming that passing a string literal is the cause of error. 我假设传递字符串文字是导致错误的原因。 What could possibly be wrong with the code? 代码可能有什么问题?

Before the first read_matrix() call I added a printf statement printf("check\\n"); 在第一次read_matrix()调用之前,我添加了一条printf语句printf("check\\n"); The printf call now started throwing a segmentation fault. 现在,printf调用开始引发分段错误。

I think it may happen because you allocate struct matrix on stack. 我认为可能会发生,因为您在堆栈上分配了struct matrix each matrix is around 4MB, so 12MB on stack. 每个矩阵约为4MB,因此堆栈为12MB。 I guess it creates site (stack overflow). 我猜它创建了站点(堆栈溢出)。

Try using static variables for matrices or dynamically allocate them. 尝试将静态变量用于矩阵或动态分配它们。 It may solve your problem. 它可以解决您的问题。 And if it doesn't, never allocate 12MB of structures on stack... 如果没有,请不要在堆栈上分配12MB的结构...

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

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