Home > Software design >  C Stack around variable corrupted and issue with the size of an array
C Stack around variable corrupted and issue with the size of an array

Time:10-02

I have a program that has been stumping me for the past few weeks. It asks the user to input how many rolls they want from two six sided dice, then runs a function that rolls those numbers and adds them together. The sums go into an array and then from that array, the amount of each sums is counted. Next, the odds of each roll is calculated as well as the error percentage. Finally, the total sums rolled, the odds, and error percentage is tabulated.

There are two issues I'm facing at the moment. The first is that I input an amount of times I want the program to roll the dice, but with larger numbers the array doesn't hold all of them. For example I input 100 and it only gives me 5 array values or with 50 it gives only 7.

My second issue is that I have a do while loop for after the program takes the input and shows the output, the user is asked if they want to try again (putting in 1 for yes or 0 for no). When I hit zero to close the program, it tells me the "stack around the variable 'countingArray' was corrupted."

I tried changing the for loop that fills the array with adding countingArray[] ; but that only told me that the stack around doAgain was now corrupted instead of countingArray. I also tried using vectors since it would be easier with me not knowing the initial size of the array, but I was struggling with that and someone else told me to use arrays instead that it would be "easier".

CODE:


#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm> 

using namespace std;

int dice(int total)
{
    int diceOne, diceTwo;

    diceOne = rand() % 6   1;
    diceTwo = rand() % 6   1;

    total = diceOne   diceTwo;

    return total;
} //dice end

int main()
{
    //seeding
    srand(time(0));

    //variables
    int input = 0, diceOne, diceTwo, total = 0, doAgain;
    int two = 0, three = 0, four = 0, five = 0, six = 0, seven = 0, eight = 0, nine = 0, ten = 0, eleven = 0, twelve = 0;

    do {
        //input 
        cout << "Please enter the number of rolls you want: ";
        cin >> input;

        //array
        int countingArray[] = { 0 };
        int oddsNow[] = { 0 };
        int odds[] = {
            2, 3, 4, 5, 6, 7,
            3, 4, 5, 6, 7, 8,
            4, 5, 6, 7, 8, 9,
            5, 6, 7, 8, 9, 10,
            6, 7, 8, 9, 10, 11,
            7, 8, 9, 10, 11, 12
        }; //odds Array end

        //filling array
        for (int rolls = 0; rolls < input; rolls  ) {
            countingArray[rolls] = dice(total);
        } //for end

        for (int i = 0; i < input; i  ) {
            cout << "\ncountingArray[" << i << "]: " << countingArray[i];
        } //for end

        //repeat
        cout << "\nTry Again? (1 == yes || 0 = exit) \n";
        cin >> doAgain;
    }while (doAgain == 1 && doAgain != 0);

    return 1;
} //main end

CodePudding user response:

You're using finitely sized arrays, in particular int countingArray[] = { 0 }; is an array with enough memory for one integer.

When you fill the array, you are indexing into whatever memory lies beyond the end of the array! This is totally scribbling over who knows what.

for (int rolls = 0; rolls < input; rolls  ) {
   countingArray[rolls] = dice(total); // What if rolls is > 0 ? BOOM!
}

If you use std::vector<int> instead of an array, you can simply push entries onto the end of it with vector.push_back(). You don't necessarily need to know how large it will grow beforehand, as it will grow as needed.

Explicitly:

std::vector<int> countingVec;
for ( int rolls = 0; rolls < input;   rolls ) {
   countingVec.push_back( dice( total ) );
}
  •  Tags:  
  • c
  • Related