This is my class:
class counters {
// variables
public:
bool isUsed;
int counterNumber;
int eventType;
char eventDescriptor[256];
// methods
public:
counters();
void setCounterNumber(int counterNumber);
void setEventType(int eventType);
void setEventDescriptor(const char* eventName );
};
this is my constructor for the class:
counters::counters()
{
isUsed=true;
counterNumber=0;
eventType=0;
strcpy(eventDescriptor,"");
}
This is my declaration of the array:
printf("There are %d counters\n",numberOfCounters);
counters pCounters[numberOfCounters];
int i;
for (i=0;i<9;i++)
printf("%d: Is this valid: %d\n",i,pCounters[i].isUsed);
The variable numberOfCounters=(rval>>11)&31; which is 6 in this case is set above. I check to see if the counter is being used and then I do some useful work if it is. My issue is that when I loop through to check if it is used/true it allows me to access array elements that aren't valid and it returns positive integers which is interpreted as true making my test to see if its used not valid. I know there must be something I'm not understanding otherwise my code would work. Shouldn't I be getting an error when trying to access an array element outside the range?
Output:
There are 6 counters
0: Is this valid: 1
1: Is this valid: 1
2: Is this valid: 1
3: Is this valid: 1
4: Is this valid: 1
5: Is this valid: 1
6: Is this valid: 160
7: Is this valid: 0
8: Is this valid: 0
numberOfCounterswhy are you not using that for the upper bound of the for loop?while( pCounters[i].isUsed == true )but I was experiencing some unexpected behavior and this for loop is my attempt to understand why.