Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access Violation

Status
Not open for further replies.

WarrenB

Programmer
Feb 7, 2001
38
GB
Ok heres a program, I won't go into great detail about what it does but the general problem is it is generating an access violation, heres the code:

#include <iostream.h>
#define MAX 500

class dip {

private:
int diparray[MAX];
int people;
int words;
int convar;
int covar;
int recvar;
int startvar;
int dipcount;
int maxcount;

public:
dip (int invar1, int invar2);
void calc (void);
void sort (void);
void output (void);
int complete;
};

dip::dip (int invar1, int invar2)
{
people = invar1;
words = invar2;
dipcount = 1;
complete = 0;
recvar = 0;
maxcount = words - 1;

//Place people into array for functioning
for (covar=1; covar<=people; covar++)
{
diparray[covar] = covar;
}

}

void dip::calc(void)
{
//Find person to start with
recvar++;
while (diparray[recvar]==0)
{
recvar++;
}
recvar=startvar;
recvar=recvar - 1;

for (convar=1; convar<=words; convar++)
{
recvar++;
if (recvar > people)
{
//Reset dip once the last person is reached
recvar = startvar;
}

if (diparray[recvar] == 0) // Debugger marks the error here
{
while (diparray[recvar] == 0)
{
recvar++;
if (recvar > people)
{
recvar = startvar;
}
}
}
}


//Person to remove is known as recvar, unless recvar is last person left check
if (dipcount == maxcount)
{
complete = 1;
}

dipcount++;

}

void dip::sort(void)
{
while (complete != 1)
{
//Make removed person in the control array = 0
diparray[recvar] = 0;
dip::calc();
}

cout << &quot;Person &quot; << recvar << &quot; wins the dip&quot;;
cout << &quot;\n\n\nEND OR PROGRAM&quot;;

}

void main(void)
{
int x;
int y;

cout<<&quot;Enter Number of People: &quot;; cin>>x;
cout<<&quot;Enter Number of Words in Dip: &quot;; cin>>y;
dip check(x,y);
check.calc();
check.sort();
}
***
Now no errors or warning are displayed upon compile. Anyone know what the problem is, it seems to have something to do with the array diparray. Thanks in advance

Warren Brown
wazzer@btinternet.com
 
Well, you're accessing the array out of bounds somewhere. If max is equal to 5, then there's diparray[0] through [4], which is extremely important. If you try to access diparray[5], you get an access violation. Track down where you're doing this, and you're home free.

I can't tell from what you posted where the violatoin is, but that's what's happening. What you can do is print a variable just before you use it as an idex:

i.e.
cout << person;
diparray[person];

And you find the one that is >= MAX and you've got it!

MWB.

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top