c++stack-smash

Stack Smashing Detected C++ after I added new variables in my code along with some operations


I made a code for computational purpose. Everything was going well until I added these lines in my function in main code. Most of the threads I read about this issue have solution referring to cases where strings are being handled. None of them relates much closer to problem I am dealing with. code added in main program is:

float flx[3][4];

for (int a0 = 0; a0 < 4; a0++) {
    for (int b0 = 0; b0 < 3; b0++) {
        cout << "fluz!!!!";
        flx[a0][b0] = phi1[a0][b0] - phi2[a0][b0];
        cout << flx[a0][b0] << " ";
    }
    cout << "\n";
}

The code terminates right after performing this part due to which further operations can't be continued.

Please help. Thanks


Solution

  • for (int a0=0;a0<4;a0++){
       for (int b0=0;b0<3;b0++){
          flx[a0][b0] = [...];
    

    Note that in the outer loop, a0 takes on values 0 to 3, inclusive, but you declared your array like this:

    float flx[3][4];
    

    ... which means only 0, 1, and 2 are valid indices to the first/major dimension of flx. You are therefore writing outside the bounds of your array, hence the stack-smashing error message.