Error: stack Smashing Detected

Asked

Viewed 216 times

0

I’m having the mistake:

* stack Smashing Detected *: terminated in my program

Use the compiler g++ (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, should return C, but it returns F.

1 answer

0


This code makes no sense. It’s declaring two arrays (the C way and not the C++way) with size 3 each. So the elements go from 0 to 2 in each.

Then read data on the console and have it stored at position 3 of each array, Only that position isn’t reserved for him, so he’s crushing the pile where that die should be. It may not give problem in some situation, but could give in another, it is dangerous. Probably wanted to keep the data in position 2, which is the last.

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

In addition to resolving the problem of the limit of array would need to solve the conceptual question of calculus.

  • Poxa thanks, I had forgotten that the arrays start at 0, I made the corrections and I got.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.