Stack error on variable corrupted? explaination please

Stack around the variable storehighlowtemp corrupted. First of all, what does this error even mean? To start of I have made a two dimensional array that will store in the high and low temperature of each month. column 0 will all consist of high temps. here is code for this
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
void getdata(unsigned int sto_highlow[][high_Low], int months)

{
	//high_low can only store in two. make a variable
	

	for (int whatmonth=1;whatmonth<=months;whatmonth++)
		{
			
			
			
			switch (whatmonth)
				{
			case 1: 
				{
				for(int high_lowtem=0;high_lowtem<2;high_lowtem++)
				{
					cout<<"High Temp of Jan: ";
					cin>>sto_highlow[whatmonth][high_lowtem];
					high_lowtem++;
					cout<<"Low Temp of Jan: ";
					cin>>sto_highlow[whatmonth][high_lowtem];
				}

				
				}
				break;
			case 2:{
				for(int high_lowtem=0;high_lowtem<2;high_lowtem++)
				{
					cout<<"High Temp of Feb: ";
					cin>>sto_highlow[whatmonth][high_lowtem];
					high_lowtem++;
					cout<<"Low Temp of Feb: ";
					cin>>sto_highlow[whatmonth][high_lowtem];
				}
				
				   }
				   break;
			
		}
}


Overhere I have a function that will see the hightest temperature of the year.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int high_tempofyear(unsigned highlowtempstored[][high_Low],int months)
{
	int hightemp=highlowtempstored[1][0];
	int insidemax=0;
	for (int row =2;row<=months;row++)
	{
		for(int col=0;col<1;col++)
		{
			insidemax=highlowtempstored[row][col];
				if (hightemp<insidemax)
					hightemp=highlowtempstored[row][col];
		}
	
	}
	return hightemp;
}


Lastly, here is my main function where I'm getting the error from saying stack around the variable storehighlowtemp corrupted. Doesn't specify the line but when im' running the program it works up to "SUM" and outputs the result of that. but after that that's when I get an error. Can someone explain to me wh is that please?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	
	unsigned int store_high_Lowtemp[months][high_Low];
	//Make a variable for high temp and low temp
	int high;
	int low;
	//call function to store in high and low temps
	getdata(store_high_Lowtemp,months);

	cout<<endl;
	//find the average high of the year
	cout<<"SUM: ";
	int sum;
	cout<<average_high(store_high_Lowtemp,months);
	
	cout<<"\n\n\n\tHigh Temp: ";
	high_tempofyear(store_high_Lowtemp,months);
	
}


line 16 is where the error occurs
Last edited on
I'm pretty sure you have an off by one error in your for loop:
for (int row =2; row <= months; row++)
It should be:
for (int row = 2; row < months; row++)

Your array has one dimension of size months, so the indices for your rows will be between and months - 1.

The error means that you have overwritten memory that you were not supposed to change, and the program detected it. This happens because C++ applies no bounds checking on memory accesses, and so it will happily modify memory for you even if you've gone out-of-bounds.
Last edited on
yulingo, I did fix the potential error you told me however It still tells me there is a "stack error of variable store-hightemplowtemp corrupted" :(

I could be wrong here, but the error you pointed out is correct in my opinion, meaning my original code. There are 12 months and I'm starting at 1. If I were to start at zero then it would have only been a < sign. Contrasly, over here, I'm starting at 1. Once again, not saying I'm right and you could be right too, but this is my opnion :)
If you are trying to represent months from 1 to 12 with an array with only 12 rows, then you have to convert from month to array index, which you are not doing. Right now store_high_Lowtemp[12][0] is out-of-bounds of the array. You can only access rows 0-11. Either that or you make an array with 13 rows and use 1-12 and never use row 0.
Topic archived. No new replies allowed.