Run-Time Check Failure

I wrote the code below, and I keep getting the error 'Run-Time Check Failure #2 -Stack around the variable 'index' was corrupted.
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
//in DblArray.h:
#include <iostream>
using namespace std;

const int maxcells = 5;                         

class DblArray
{
public:
	DblArray();
	void Set (int i, double val);
	double Get (int i) const;
	bool IsSet (int i) const;
private:
       	double indexarray[maxcells];
	int setrecord[maxcells];
};
 

//In DblArray.cpp:
#include <cassert>
#include "DblArray.h"

DblArray::DblArray()
{
	for(int i = maxcells;i>=0;i--)
	{
		setrecord[i]=0;
	}
};
void DblArray::Set (int i, double val)
{
	assert (IsSet(i) == false);
	assert (i<=(maxcells-1));
	indexarray[i] = val;
	setrecord[i] = 1;
};

double DblArray::Get (int i) const
{
	assert (IsSet(i)==true);
	return indexarray[i];
};

bool DblArray::IsSet (int i) const
{
	if (setrecord[i]==0)
	{return false;}
	else
	{return true;}
};


//In tes.cpp to test the code above.
#include "DblArray.h"

int main()
{
      DblArray index;
      index.Set(4,5.67);
      index.Set(3,3.32);
      cout<<"In cell four: "<<index.Get(4)<<"\n";
      cout<<"In cell three: "<<index.Get(3)<<"\n";
      system("pause");
}


Does anyone know how I can correct this? (I'm compiling it using Visual C++ 2008 express edition.)
Last edited on
i had the same problem, i believe it was because i set my array too large
perhaps that will help you?
Sure enough, when I entered 5 for maxcells, it worked just fine. (5 seems to be the magic number. Any more than that, and I get the error.) But why? Does it have something to do with a limit on memory that is allowed to be allocated to a given array?

Last edited on
yeah it depends on the compiler, when i use MS Visual Studio, you have to go into properties and make the stack and heap larger so that you may use larger arrays. or in other words, you're giving your program more room to operate.

you say you're using visual studio?
Yes. Do you know how to adjust the stack and heap allocations?
Program properties => Configuration => Linker => System
set heap to a larger amount
set stack to a larger amount

Edit: I use 2005 still, but i'm sure you still do the same thing.
Last edited on
I see Heap Commit Size 0, Stack Commit Size 0, and the reserve sizes for both are set to 0 as well. What's typically considered a decent amount? Are these specified in bytes? I tried entering as much as thirty, and it still gave me the same result. (I have been able to compile it in Dev-C++ with no problems though.)
im not too sure, we covered this problem in lecture today. maybe try a larger number than 30? as far as if they're specified in bytes im not sure either. before being told how to solve the problem i just went and compiled it on linux. if you're still having a problem by the time i get home later on tonight i'll check for you
Well, I went all the way up to 1000000, and I still got the error. (It's also returning the wrong value for cell 4 now, even if I change it back to 0.) I don't get it. This code executes just fine in Dev-C++. Any other suggestions?
Topic archived. No new replies allowed.