0

I get a bus error 10 from this code. When I put in print statements, I get a segmentation fault error. I use these two functions in a larger program to compute the determinant of the matrix. What do these errors mean? I am new to the C environment. Thanks!

int* cofactor(int* matrix, int co_row, int co_column, int size_of_matrix){
 int* result;
 int i, offset;
 int row, column;

 result = duplicate_matrix(matrix, size_of_matrix-1);
 i = 0;
 offset = row*size_of_matrix+column;
 for (row = i; row < size_of_matrix; row++){
  for (column = i; column < size_of_matrix; column++){
   if ((row != co_row) && (column != co_column)){
    i = offset;
    i += 1;
    *result = i;
   }
   return result;
  }
 }
}

int* determinant(int* matrix, int size_of_matrix){
 int sign, x, one=1;
 int* size;
 int* result;
 int* the_sign;
 int* comatrix;

 sign = one;

 *the_sign = sign;
 *size = size_of_matrix;
 result = malloc(sizeof(int) * size_of_matrix*size_of_matrix);
 comatrix = malloc(sizeof(int) * size_of_matrix*size_of_matrix);
 if (*size == one){
  return size;
 }
 else{
  for (x = 0; x < size_of_matrix; x++){
    comatrix = cofactor(matrix, 0, x, size_of_matrix);
    *result = x;
    *result += *matrix;
    *matrix *= *the_sign;
    *the_sign *= *determinant(comatrix, size_of_matrix);
    *the_sign *= -1;
    *result = *the_sign;
  }
  return result;
 }
}
  • 2
    Those errors mean your pointers are all messed up and don't do what you think they do. `*the_sign = sign;` for example, crashes your program because you are dereferencing a pointer you haven't initialized. – indiv Apr 25 '14 at 01:22
  • There are quite a few issues with the code: `offset = row*size_of_matrix+column` at that point row and column are not initialized; `*result = i` result is never changed, so you are always writing to one location; `return result` is inside the for loop. I've only looked at the first function. – Brad Budlong Apr 25 '14 at 01:37
  • It is hard to see how `duplicate_matrix()` can avoid doing dynamic memory allocation with `malloc()` — yet there is no code to free any of the matrices created by it. That leaves you with a problem — either invalid code in `duplicate_matrix()` or a memory leak. – Jonathan Leffler Apr 25 '14 at 01:43
  • What do you think `*the_sign = sign;` is doing? – M.M Apr 25 '14 at 02:00

1 Answers1

1

There is nice SO post about what us the difference between the bus error fault and segmentation fault.

https://stackoverflow.com/a/212585/2724703

They are generally means that your program is not well designed and due to some reason you are trying to access the memory which is not legal. In your program there is number of problems which should be rectified in order to work. As:

Here your program is trying to access/write into uninitialized pointer and hence can lead to either bus/segmentation fault. Your program seems to using pointer instead of normal variable where it is not required.

//These variables should be defined as normal and not pointer.
int size; int the_sign;

I think you should also starts looking about your compiler warning. In these scenario, compiler should give you warning similar to gcc/g++ in the sample program. Please pay attention to them and rectify these warning message from your program.

int* i;
int j;

*i = j;

$ g++ -Wall foo.cpp foo.cpp: In function ‘int main()’: foo.cpp:13:7: warning: ‘i’ may be used uninitialized in this function [-Wmaybe-uninitialized] *i = j; ^ foo.cpp:13:7: warning: ‘j’ may be used uninitialized in this function [-Wmaybe-uninitialized]

Additionally you need to free the memory which your program has allocated by malloc. Otherwise there would be memory leak.

Community
  • 1
  • 1
Mantosh Kumar
  • 5,659
  • 3
  • 24
  • 48