# c++ VC2015. Run-Time Check Failure #2 - Stack around the variable 'house' was corrupted.

//// Cops and the Thief Devu
//
//All submissions for this problem are available.
//
//Read problems statements in Mandarin Chinese and Russian as well.
//
//There are 100 houses located on a straight line.The first house is numbered 1 and the last one is numbered 100. Some M houses out of these 100 are occupied by cops.
//Thief Devu has just stolen PeePee's bag and is looking for a house to hide in.
//PeePee uses fast 4G Internet and sends the message to all the cops that a thief named Devu has just stolen her bag and ran into some house.
//Devu knows that the cops run at a maximum speed of x houses per minute in a straight line and they will search for a maximum of y minutes.Devu wants to know how many houses are safe for him to escape from the cops.Help him in getting this information.
//Input
//
//First line contains T, the number of test cases to follow.
//First line of each test case contains 3 space separated integers : M, x and y.
//For each test case, the second line contains M space separated integers which represent the house numbers where the cops are residing.
//Output
//
//For each test case, output a single line containing the number of houses which are safe to hide from cops.
//Constraints
//
//1 ≤ T ≤ 104
//1 ≤ x, y, M ≤ 10
//Example
//
//Input :
//3
//4 7 8
//12 52 56 8

//2 10 2
//21 75

//2 5 8
//10 51
//
//Output:
//0
//18
//9
//Explanation
//
//Example 1 : Cops in house 12 can cover houses 1 to 68,
// and cops in house 52 can cover the rest of the houses.
// So, there is no safe house.

//Example 2 : Cops in house 21 can cover houses 1 to 41,
// and cops in house 75 can cover houses 55 to 95,
// leaving houses numbered 42 to 54, and 96 to 100 safe.
// So, in total 18 houses are safe.

``````#include <iostream>
using namespace std;
int main() {
int t;
cin >> t;
int cops_n, speed, time;

while (t--) {

cin >> cops_n >> speed >> time;

int  sum = 0, input;
int house[100];
int *occup_houses = new int[cops_n];

for (int i = 0; i <= 100; ++i) {
house[i] = 1;
}

for (int i = 0; i < cops_n; ++i) {
cin >>input  ;
occup_houses[i] = input - 1;
if (occup_houses[i] - speed*time < 0) {
for (int j = 0; j <= occup_houses[i] + speed*time ; j++) {

if (j < 100)
house[j] = 0;
else
break;
}
}
else
for (int j = occup_houses[i] - speed*time;
j <= occup_houses[i] + speed*time ; j++) {
if (j < 100)
house[j] = 0;
else
break;
}
}

for (int i = 0; i < 100; i++)
sum += house[i];

cout << sum << endl;

}

return 0;
}
``````

When last case is culculated. I have some run time error

" Run-Time Check Failure #2 - Stack around the variable 'house' was corrupted."
I use Visual Studio 2015 community

please could you tell me how can I resolve this error.
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Hi sunnyx,

the problem is that you write over the bound of an array i.e. with:
``````		for (int i = 0; i <= 100; ++i) {
house[i] = 1;
}
``````
Since house is defined as an array with 100 elements its valid indicies are between 0 and 99, but the for loop you use tries (with the last call) to write '100' into house[100] which leads to writing over the bounds in memory.

I guess you intended to use for (int i = 0; i < 100; ++i) instead.

Hope this helps,

ZOPPO

Experts Exchange Solution brought to you by