are global arrays dynamic ?

Hello, I'm trying to understand a piece of code I ran into. AS far as I knew, if you define an array of a certain size, then it could not be resized at execution time.

So what I do in the following code is define a global array with a size of 10, and then in the main function I initialize up to index 12, and yet get no errors and expected output. Can't tell what's going on. Hope someone could explain it, I've been researching but no luck yet. Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
int array[10];
int main() {
	int i = 0;
	for(i=0; i <=12; i++) {
		array[i] = 0;
	}

	for(i=0; i <=12; i++){
		cout << "array[" << i<< "]=" << array[i] << endl;
	}
}


The output I get is:

array[0] = 0
array[1] = 0
array[2] = 0
array[3] = 0
array[4] = 0
array[5] = 0
array[6] = 0
array[7] = 0
array[8] = 0
array[9] = 0
array[10] = 0
array[11] = 0
array[12] = 0

Thanks a lot in advance.
That causes undefined behavior. If it works, it's simply because you are lucky, since array[9]-array[12] don't belong to you and could be anything.
Alright, thanks a lot for taking the time to answer ! Really appreciate it.

Regards
Topic archived. No new replies allowed.