Error: stack smashing detected

I am having the error:

* stack smashing detected * : terminated in my program

I use the G ++ compiler (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0, here is the Code:

#include    <iostream>

using namespace std;

int main()<br>
{
    int C[3], F[3];<br>
    int Ct,Ft;<br>


    cin >> C[3] >> F[3];

    Ct = (C[1] * 3)+C[2];
    Ft = (F[1] * 3)+F[2];

    if ((Ct > Ft) || (Ct == Ft && C[3] > F[3]))
    {
        /* code */
        cout << "C" << endl;
    }else if ((Ft > Ct) || (Ft == Ct && F[3] > C[3]))
    {
        /* code */
        cout << "F" << endl;
    }else
    {
        cout << "=" << endl;
    }




    return 0;
}

The input I put is: 10 5 18 11 1 18, it should return C, but it returns F.

Author: Maniero, 2020-04-17

1 answers

This code doesn't make sense. It is declaring two arrays (C-Way and not c++ - way) with size 3 each. Then the elements go from 0 to 2 in each.

Then has data read in the console and has it save at Position 3 of each array , only that position is not reserved for it, so it is crushing the stack where this data should be. It may not give problem in some situation, but it could give in another, it is dangerous. Probably wanted to save the given in position 2, which is the last.

Then takes data contained in position e 1 and 2 of the array , probably wanted to take 0 and 1. But even if you fix this is still a problem because these positions have not been initialized, then it will pick up garbage from memory and the calculation will probably give the wrong result.

In addition to solving the array limit Problem it would need to solve the conceptual question of calculus.

 0
Author: Maniero, 2020-04-23 17:10:03