Array index variable incrementing reset implicitly

Hello. I am learning about c++ these days.

When I build the code below, I expect out of index error when the count variable over 11, but it didn’t.

What makes this a kind modular operation?

c++ itself? or somewhere on openframeworks?
Is there something that I miss?

Have a nice day.

// These are global. (ofApp.h)
ofPoint path [12];
int count = 0;
int cc = 0;

// ofApp.cpp
void ofApp::mouseDragged(int x, int y, int button){

    ofPoint p = ofPoint(x, y);
    path[count] = p;
    cout << count << " / " << cc << endl;

Hi @giy_hands,

Compared to languages using a virtual machine - like Java based languages - which can check array boundary access at runtime, C++ is a purely compiled language. A C++ array is a pointer (an address) to a memory location, the bracket semantic is a helpful way to specify an offset of memory blocks of the size of the underlying type from that pointer.

Equivalent expressions :

path[count] = p;

*(path + count) = p;

void *addr = path;
*((ofPoint*)(addr) + count * sizeof(ofPoint)) = p;

Since C++ 11 you can also use the STL container <array> to check boundary at compile time when you have a constant expression :

#include <array>

int main() {
   std::array<int, 8> myArray;
   int index = 9;
   myArray[0] = 0;       // Ok.
   myArray[8] = 5;       // compile time error.
   myArray[index] = 0;   // possible runtime error.

Accessing out of boundary array memory block in C++ will not always result in a runtime error, but you can use the STL containers at() method to add runtime check.

See std::vector<T,Allocator>::at -

1 Like

Thanks @DarkSalmon.

But my question was that
why the count variable reset to 0 automatically when it reachs 11.

I have some test about it.
When I set exactly 2 variables before count variable on ofApp.h, it happens.
The name of variables doesn’t matter.

int x;
int y;
int count;


float salkdgasx;
float yaslkdf;
int counttt;

How does that happens?
You can test it with files attached.

main.cpp (344 Bytes)
ofApp.cpp (1.8 KB)
ofApp.h (701 Bytes)

if you remove (not comment out, remove) the following lines from the ofApp.h file it will fail with when the index goes out of bounds and it will not get reseted.

    int aalksgdj;
    int bgasedasf;

What I think is happening is that when the index is 12 and you try to set it to the value of the mouse it will go out of bounds of the array but that will fall to the address of the countt variable which gets set to zero. You would need to set some breakpoints there and inspect what is actually going on.

1 Like

Hey @giy_hands,

As @roymacdonald suggested, and deriving from pointers algebra as noted above, when your array reaches its boundaries it will write in subsequent memory blocks.

An ofPoint is 12 bytes of memory, a int 4 bytes.

So when you put an ofPoint in path[13] it will wrote on aalksgdj, bgasedasf and countt.

Hopes that clarifies everything,


Thank you!
You make my mind very clear :slight_smile: :smiley: