简体   繁体   English

c:进程以退出代码 139 结束(被信号 11 中断:SIGSEGV)

[英]c: Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

I am making a program that can make an array from user input and save it inside a csv files.我正在制作一个程序,该程序可以从用户输入中创建一个数组并将其保存在 csv 文件中。 Then the user is able to perform matrix and vector operations with the stored arrays.然后用户可以使用存储的 arrays 执行矩阵和向量运算。 When i started working with csv files(initially on clion) i started getting the error: "Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)" After debugging i found the error "EXC_BAD_ACCESS (code=2, address=0x7ff7b202dff8)".当我开始使用 csv 文件(最初在 clion 上)时,我开始收到错误消息:“进程以退出代码 139 完成(被信号 11 中断:SIGSEGV)”调试后我发现错误“EXC_BAD_ACCESS(代码=2,地址=0x7ff7b202dff8 )”。 However the program still worked when ran in replit.但是,该程序在重新运行时仍然有效。 Sadly though after i wrote lines 60 -65 in order to make a different csv file every time the function is ran i started getting the error: "signal: segmentation fault (core dumped) " on replit.可悲的是,尽管每次运行 function 时我写了第 60 -65 行以便制作不同的 csv 文件后,我开始收到错误消息:“信号:分段错误(核心转储)”在重新打印时。 What could be causing these errors?什么可能导致这些错误? Here is my code on replit:这是我的repli代码:

"https://replit.com/@iasonaszak/the-matrix#main.c" “https://replit.com/@iasonaszak/the-matrix#main.c”

Thank you in advance for your help!!预先感谢您的帮助!!

and here is my code:这是我的代码:

    #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "time.h"
#include <math.h>
#include <stdbool.h>
int a;
char c[20];

int num;


bool b = true;
int main() {
  
  while (b ==true){
    void create_array();
    void delete_array();
    int user_answer;
    time_t t;
    srand((unsigned) time(&t));

    printf(
            " 1.create an array\n 2.load an array\n 3.Show available arrays\n 4.Delete array\n 5.Vector operations \n 6.Matrix operations"

    ) ;
    printf("Please choose one of the following options!\n");
    scanf("%d" , &user_answer);

    if (user_answer == 1){
        create_array( );
    }
     else if (user_answer == 4){
       delete_array();
  
}
    return 0;
}

}
void create_array(){
    int rows;
    int cols;
    int i;
    int j;
    int matrix[i][j];
    printf("how many columns would you like your array to have");

    scanf("%d" ,&cols );
    printf("how many rows would you like your array to have");
    scanf("%d" ,&rows );
     

 char filename[80];
  
  FILE *fout = fopen(filename, "wt");
  FILE *last = fopen("lastnum.csv" , "w");

    fgets(c , 20 , last);
  num = atoi(c);

  snprintf(filename, sizeof(filename), "prefix.%d.csv", num);
  
  
    for (i = 0 ; i < rows; i ++){
      printf("\n");
      fprintf(fout , "\n");
        for (j = 0 ; j < cols; j ++){
            scanf("%d", &matrix[i][j]);
            
            char str[(int)((ceil(log10(matrix[i][j]))+1)*sizeof(char))];
            sprintf(str , "%d " , matrix[i][j]);
            fprintf(fout ,"%s" , str);
        }
        
      
        
    }
printf("Your array was saved successfuly inside a csv file");
num++;
        



}
void delete_array(){
  remove("prefix.0.csv");
   remove("prefix.1.csv");
    remove("prefix.2.csv");
     remove("prefix.3.csv");
      remove("prefix.4.csv");
       remove("prefix.5.csv");
  remove("prefix.6.csv");
   remove("prefix.7.csv");
    remove("prefix.8.csv");
     remove("prefix.9.csv");
  

  printf("all arrays were deleted");
}

void preview_arrays(){
  FILE *F0 = fopen("prefix.0.csv" , "r");
  
}

These three lines will cause a problem:这三行会导致一个问题:

char str[(int)((ceil(log10(matrix[i][j]))+1)*sizeof(char))];
sprintf(str , "%d " , matrix[i][j]);
fprintf(fout ,"%s" , str);

The expression (int)((ceil(log10(matrix[i][j])) + 1 will produce 1 from a matrix value of 1 , but char str[1]; won't be enough for the nul-terminated string.表达式(int)((ceil(log10(matrix[i][j])) + 1将从矩阵值1 1char str[1];对于以 nul 结尾的字符串来说是不够的.

And when the matrix value is 0 the logarithm is undefined.当矩阵值为0时,对数未定义。 In my test it attempted to define char str[-2147483648];在我的测试中,它试图定义char str[-2147483648];

You don't use str for anything else, so I suggest you remove those three lines and use this one simple line instead:您不要将str用于其他任何事情,因此我建议您删除这三行并改用这一简单行:

fprintf(fout,"%d ", matrix[i][j]);

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

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