-1

I'm having a problem in running the following code. It is giving me a segmentation fault as a runtime error.

#include <iostream>
using namespace std;

int main()
{
    int n;
    cout << "Enter n: ";
    cin >> n;

    float A[n][n], x[n], B[n], L[n][n], U[n][n], m[n][n], Aug[n][n + 1];

    //Initializing matrix A,L,U
    cout << "Enter A: \n";
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> A[i][j];
            U[i][j] = A[i][j];
            Aug[i][j] = A[i][j];
            if (i == j) {
                L[i][j] = 1;
            }
            else {
                L[i][j] = 0;
            }
        }
    }

    //Initialising matrix B
    cout << "Enter B: \n";
    for (int i = 0; i < n; i++) {
        cin >> B[i];
        cout << "done" << i;
        Aug[i][n] = B[i];
    }

    // ...

    return 0;
}

Input:

n=2, A={2,5,-3,-4}, B={0,0}

The error occurs when I try to input B[1] so done0 gets printed however done1 doesn't. I just can't figure out what the reason of this error is as I don't see any reason for B[1] to be inaccessible.

Azeem
  • 11,148
  • 4
  • 27
  • 40
oshhh
  • 141
  • 2
  • 10
  • What is your compiler? – Azeem Aug 25 '18 at 07:47
  • @Azeem I tried compiling it on both terminal (Mac) and the online IDE of Codechef – oshhh Aug 25 '18 at 07:47
  • With GCC or Clang on Mac? – Azeem Aug 25 '18 at 07:48
  • @Azeem I used gcc – oshhh Aug 25 '18 at 07:49
  • @Azeem can the reason for segmentation fault be the large amount of space taken up by the program? Although Its not really taking too much space but I do have a lot of matrices..I wasn't getting an error earlier, with fewer matrices – oshhh Aug 25 '18 at 07:51
  • Right. You are using Variable length arrays (VLAs) which are non-standard. Only GCC supports this. I'm not sure if you are aware of it or not. You should use `std::vector` instead of VLAs. – Azeem Aug 25 '18 at 07:51
  • @Azeem but gcc does support VLAs, so whats the problem? – oshhh Aug 25 '18 at 07:53
  • Well, it's a non-standard code. GCC supports it only as an extension. See: https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html. You might want to use the debugger to trace what is happening with your array. Plus, update your question with the complete error. That would be helpful. – Azeem Aug 25 '18 at 07:59
  • @Azeem complete error? It only shows Segmentation fault: 11 – oshhh Aug 25 '18 at 08:05
  • 2
    @OsheenSachdev _"gcc does support VLAs, so whats the problem?"_ That you could overflow the stack with large arrays easily. – πάντα ῥεῖ Aug 25 '18 at 08:06
  • 2
    "What's the problem". Well if you used standard C++ then people without access to gcc could help you with this problem. It's not good to use non-standard code without good reason. – john Aug 25 '18 at 08:08
  • @John By what's the problem I was asking about the problem that's giving an error in my code :) – oshhh Aug 25 '18 at 08:21

1 Answers1

3

You've misdiagnosed the problem. The segmentation fault occurs after the for loop finishes, in the code you haven't shown us (the // ... part). The done1 doesn't get printed because your code faults before it has a chance to flush the output buffer. Your cout << "done" << i; just puts things in the output buffer, there's nothing there to flush the buffer.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278