'the stack around variable '...' was corrupted.'

Hi there,

I am attempting to write a few basic card-game programs, and to do so, I thought I'd generate a deck of cards. I've provided the code which I've written to do so below:

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <time.h>
#include <string>

using namespace std;

void gen_cards()
{	
	srand(time(NULL));
	int card[2];
	for (int counter = 0; counter <= 1; counter++)
		card[counter] = 0;
	
	int deck[53];
	for(int counter = 0; counter <= 53; ++counter)
		deck[counter] = counter;
	
	string card_output[2];
	int counter = 0;

	while (deck[card[0]] == 0)
		card[0] = rand()%53;
	deck[card[0]] = 0;
	
	while (deck[card[1]] == 0)
		card[1] = rand()%53;
	deck[card[1]] = 0;

	for (int counter = 0; counter < 2; counter++)
	{
		if (card[counter] == 1)
			card_output[counter] = "2H";
		else if (card[counter] == 2)
			card_output[counter] = "3H";
		else if (card[counter] == 3)
			card_output[counter] = "4H";
		else if (card[counter] == 4)
			card_output[counter] = "5H";
		else if (card[counter] == 5)
			card_output[counter] = "6H";
		else if (card[counter] == 6)
			card_output[counter] = "7H";
		else if (card[counter] == 7)
			card_output[counter] = "8H";
		else if (card[counter] == 8)
			card_output[counter] = "9H";
		else if (card[counter] == 9)
			card_output[counter] = "10H";
		else if (card[counter] == 10)
			card_output[counter] = "JH";
		else if (card[counter] == 11)
			card_output[counter] = "QH";
		else if (card[counter] == 12)
			card_output[counter] = "KH";
		else if (card[counter] == 13)
			card_output[counter] = "AH";
		else if (card[counter] == 14)
			card_output[counter] = "2D";
		else if (card[counter] == 15)
			card_output[counter] = "3D";
		else if (card[counter] == 16)
			card_output[counter] = "4D";
		else if (card[counter] == 17)
			card_output[counter] = "5D";
		else if (card[counter] == 18)
			card_output[counter] = "6D";
		else if (card[counter] == 19)
			card_output[counter] = "7D";
		else if (card[counter] == 20)
			card_output[counter] = "8D";
		else if (card[counter] == 21)
			card_output[counter] = "9D";
		else if (card[counter] == 22)
			card_output[counter] = "10D";
		else if (card[counter] == 23)
			card_output[counter] = "JD";
		else if (card[counter] == 24)
			card_output[counter] = "QD";
		else if (card[counter] == 25)
			card_output[counter] = "KD";
		else if (card[counter] == 26)
			card_output[counter] = "AD";
		else if (card[counter] == 27)
			card_output[counter] = "2C";
		else if (card[counter] == 28)
			card_output[counter] = "3C";
		else if (card[counter] == 29)
			card_output[counter] = "4C";
		else if (card[counter] == 30)
			card_output[counter] = "5C";
		else if (card[counter] == 31)
			card_output[counter] = "6C";
		else if (card[counter] == 32)
			card_output[counter] = "7C";
		else if (card[counter] == 33)
			card_output[counter] = "8C";
		else if (card[counter] == 34)
			card_output[counter] = "9C";
		else if (card[counter] == 35)
			card_output[counter] = "10C";
		else if (card[counter] == 36)
			card_output[counter] = "JC";
		else if (card[counter] == 37)
			card_output[counter] = "QC";
		else if (card[counter] == 38)
			card_output[counter] = "KC";
		else if (card[counter] == 39)
			card_output[counter] = "AC";
		else if (card[counter] == 40)
			card_output[counter] = "2S";
		else if (card[counter] == 41)
			card_output[counter] = "3S";
		else if (card[counter] == 42)
			card_output[counter] = "4S";
		else if (card[counter] == 43)
			card_output[counter] = "5S";
		else if (card[counter] == 44)
			card_output[counter] = "6S";
		else if (card[counter] == 45)
			card_output[counter] = "7S";
		else if (card[counter] == 46)
			card_output[counter] = "8S";
		else if (card[counter] == 47)
			card_output[counter] = "9S";
		else if (card[counter] == 48)
			card_output[counter] = "10S";
		else if (card[counter] == 49)
			card_output[counter] = "JS";
		else if (card[counter] == 50)
			card_output[counter] = "QS";
		else if (card[counter] == 51)
			card_output[counter] = "KS";
		else if (card[counter] == 52)
			card_output[counter] = "AS";
	}


	cout << card_output[0] << endl << card_output[1] << endl << endl;
			
}

int main()
{
	gen_cards();
	return 0;
}


This code returns an error stating that 'the stack around variable "deck" was corrupted."

Being somewhat new to C++ I'm not overly confident with what this means or how to overcome it. Please excuse me if it is a very simple error as, like I have stated, I am a newbie. Also I'm sure there are many better ways to generate a deck of cards. I think the recurring if statements are most likely causing me an issue yet I'm not sure how to write the program without them.

Any help is greatly appreciated. Thanks in advance, James.
On lines 14 to 16. You declared an array of size 53 (which is the wrong number of cards in a deck) which means the valid indices for the array are from 0 to 52 (deck[0] to deck[52]). However, your for loop continues an iteration when counter = 53, so you attempt to access deck[53], which is one step out of the array boundaries. You grabbed something you weren't supposed to and modified it, corrupting the stack.

You should reconsider your design, so you don't have to use so many else if statements.
Being somewhat new to C++ I'm not overly confident with what this means or how to overcome it.


Usually it means you are stepping outside the bounds of an array. In this case, the error suggests that the array is "deck".

If you note, you are stepping out of bounds here:

1
2
3
	int deck[53]; // <- deck is size [53], which means [52] is last legal element to access
	for(int counter = 0; counter <= 53; ++counter)
		deck[counter] = counter;  // <- accessing element [53].. OUT OF BOUNDS 
Thank you both of you, it works perfectly now.
Topic archived. No new replies allowed.