0

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
4
  • 2
    If you know the size of the array is numberOfCounters why are you not using that for the upper bound of the for loop? Commented Apr 20, 2016 at 14:43
  • The program gets the numberOfCounters from a register which could differ from system to system. My original loop used this: while( pCounters[i].isUsed == true ) but I was experiencing some unexpected behavior and this for loop is my attempt to understand why. Commented Apr 20, 2016 at 14:49
  • "Shouldn't I be getting an error when trying to access an array element outside the range?" No, that's not how C++ works. All you can say is that the program may or may not do anything at all. Commented Apr 20, 2016 at 14:50
  • You're certainly getting undefined behavior. Commented Apr 20, 2016 at 14:55

2 Answers 2

1

Not necessarily, all you can say is that, when accessing an element where the index is bigger than the size, it causes undefined behaviour. See http://www.cplusplus.com/reference/array/array/operator[]/ for more info.

Sign up to request clarification or add additional context in comments.

Comments

1

Array bounds checking is not done on C-style arrays (or indeed on C++-style std::array<>s, unless you use the at member function). If you want bounds checking, you should use std::vector<>.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.