Stack around the variable corrupted?! Help!

I'm getting an error where Stack around the variable 'dicerolls' was corrupted... Can anyone offer an insight as to why this is happening? Program executes perfectly until then

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;


void run_die(int dice[], int size)
{
	bool in_run= false;
	for (int i=0;i<=19;i++)
	{
		if (in_run)
		{
			if (dice[i]!= dice[i-1])
			{
				cout<<")";
				in_run=false;
			}
		}
		if (!in_run)
		{
			if(i<19)
			{
				if (dice[i]==dice[i+1])
				{
					cout<<" (";
					in_run=true;
				}	
			}
		}
		cout<<dice[i]<<" ";
	}
	if (in_run)
	{
		cout<<" ) ";
	}
}


int main()
{
	srand(time(0));
	int dicerolls[19];
	int size=20;
	for (int i=0;i<=20;i++)
		{
			dicerolls[i-1]=rand()%6+1;
		}
	run_die(dicerolls,19);
	system("pause");
}

You're making many out of bounds references.

Line 14: If i is 0, you're going to reference dice[-1] which is illegal.

Line 24: If i is 18, you're going to reference dice[19] which is also out of bounds. dicerolls is declared as 19, so the elements are 0-18.

Line 47: Again, if i is 0, you're going to reference dicerolls[-1].

Line 44: Why are you declaring size as 20? You don't even use the variable.

Line 45: Your loop is going to run for 21 iterations. Again, when i is 20, dicerolls[19] is going to be out of bounds.

1
2
3
4
5
6
7
8
9
10
11
bool in_run= false;
for (int i=0;i<=19;i++)
{
	if (in_run) // i think this thing would never executed till the end of our universe
	{
		if (dice[i]!= dice[i-1])
		{
			cout<<")";
			in_run=false;
		}
	}
Last edited on
Thank you for your input AbstractAnon and chipp...
I will try to fix the issues you've pointed out to me and chipp, I don't quite understand what you mean... line=4 if (in_run) // That can run because if the conditions in line 27 are met to start a "run" then that would be necessary to get a ) around the end of a run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
void run_die(int dice[], int size)
{
	//Sets up a bool to detect wether or not a run is present
	bool in_run= false;
	//For loop runs to check each pairing of numbers/rolls
	for (int i=0;i<=size;i++)
	{
		//checks to see if a run is currently present 
		if (in_run)
		{
			//Keeps function in bounds
			if (i>1)
			{
				//this statement checks to see if the current run ends
				if (dice[i]!= dice[i-1])
				{
					//If the run in fact ends it outputs a ) and changes the bool to reflect current run status
					cout<<")";
					in_run=false;
				}
			}
		}
		//Checks to see if no run is currently present
		if (!in_run)
		{
			//Keeps the fucntion within the index of the array
			if(i<19)
			{
				//If there is a run starting and there is space in the array left... Then it outputs a ( and changes the run status to true
				if (dice[i]==dice[i+1])
				{
					cout<<"(";
					in_run=true;
				}	
			}
		}
		//After each "check" for runs it prints the next roll
		cout<<dice[i]<<" ";
	}
	//Checks if the last roll was also the end of a run
	if (in_run)
	{
		cout<<") ";
	}
}

//Create main function
int main()
{
	//Creates a "seed" for random generator
	srand(time(0));
	//Initializing the array to hold all the random "rolls" to be 20 integers long "0-19"
	int dicerolls[19];
	//Size parameter for the function call of printing parentheses around detected runs
	int size=19;
	//Performs the 20 "rolls" and stores them into the dicerolls array to later be utlizied in the run_die function call
	for (int i=0;i<=20;i++)
		{
			//Starting at i-1 because the first run through i=1 however the first roll is in dicerolls[0]
			dicerolls[i-1]=rand()%6+1;
		}
	//Call the run detection function with the array and size of the array as parameters
	run_die(dicerolls,size);
	cout<<endl;
	system("pause");
}


I added comments to hopefully try and convey what I was doing...
Also, I am still getting the same error but I thought I entered if statements to make sure I wouldn't make those out of bounds references... And I still can't find the issue :/
Okay, I finally figured it out and it runs with no errors! I will post the code that way you all can see that I actually got it! Thank you for the pointers, just what I needed! Thanks!!!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;


void run_die(int dice[], int size)
{
	//Sets up a bool to detect wether or not a run is present
	bool in_run= false;
	//For loop runs to check each pairing of numbers/rolls
	for (int i=0;i<size;i++)
	{
		//checks to see if a run is currently present 
		if (in_run)
		{
			//Keeps function in bounds
			if (i>1)
			{
				//this statement checks to see if the current run ends
				if (dice[i]!= dice[i-1])
				{
					//If the run in fact ends it outputs a ) and changes the bool to reflect current run status
					cout<<")";
					in_run=false;
				}
			}
		}
		//Checks to see if no run is currently present
		if (!in_run)
		{
			//Keeps the fucntion within the index of the array
			if(i<19)
			{
				//If there is a run starting and there is space in the array left... Then it outputs a ( and changes the run status to true
				if (dice[i]==dice[i+1])
				{
					cout<<"(";
					in_run=true;
				}	
			}
		}
		//After each "check" for runs it prints the next roll
		cout<<dice[i]<<" ";
	}
	//Checks if the last roll was also the end of a run
	if (in_run)
	{
		cout<<") ";
	}
}

//Create main function
int main()
{
	//Creates a "seed" for random generator
	srand(time(0));
	//Size parameter for the function call of printing parentheses around detected runs
	int size=20;
	//Initializing the array to hold all the random "rolls" to be 20 integers long "0-19"
	int dicerolls[20];
	//Performs the 20 "rolls" and stores them into the dicerolls array to later be utlizied in the run_die function call
	for (int i=1;i<=size;i++)
		{
			//Starting at i-1 because the first run through i=1 however the first roll is in dicerolls[0]
			dicerolls[i-1]=rand()%6+1;
		}
	//Call the run detection function with the array and size of the array as parameters
	run_die(dicerolls,size);
	cout<<endl;
	system("pause");
}
That can run because if the conditions in line 27 are met to start a "run" then that would be necessary to get a ) around the end of a run

my bad, didn't notice it
Topic archived. No new replies allowed.