Home > database >  c: Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
c: Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

Time:12-29

I am making a program that can make an array from user input and save it inside a csv files. Then the user is able to perform matrix and vector operations with the stored 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)" . 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. What could be causing these errors? Here is my code on replit:

"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");
  
}

CodePudding user response:

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.

And when the matrix value is <= 0 the logarithm is undefined. In my test with 0 it attempted to define 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:

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

Update

Another fault is in the file opening: wrong mode

FILE *last = fopen("lastnum.csv" , "w");

should be

FILE *last = fopen("lastnum.csv" , "r");

and always check that fopen() succeeds!

if(fout == NULL) {
    /* handle error */
}
if(last == NULL) {
    /* handle error */
}

and always check that fgets() succeeds too.

if(fgets(c , sizeof c , last)) == NULL) {  // changed the 'magic' 20
    /* handle error */
}
  • Related