Elementary array question

 
I apologize for asking such a basic question but of course documentation does not help since my question is why this does NOT generate an error, when I think it should.

   //you can also assign array values at declaration
   int myArray2[3] = {1, 2, 3};
   //recall that array indexing begins at zero so
   myArray2[4] = 4; //will cause an array out of range error--except it didn't!!

It would seem to me that attempting to access the fourth element in an array of size 3 should generate an array out of range error, and yet my code compiles. Why?
 
and yet my code compiles. Why?

Your code will compile, but it will not execute.

 
Interesting - but why would it let me compile if this is a show-stopping bug?
 
Because the compiler will only check syntax. It does not check your code for runtime errors.

 
Ah, I understand now. Thank you. Although it seems odd that such errors can't be caught by the compiler. It seems like it would be trivial?
 
clemmo: Ah, I understand now. Thank you. Although it seems odd that such errors can't be caught by the compiler. It seems like it would be trivial?

Because in MQL, arrays can be resized at runtime, so there can be code being executed in another region that would then make that line valid at runtime.

 
Fernando Carreiro:

Because in MQL, arrays can be resized at runtime, so there can be code being executed in another region that would then make that line valid at runtime.

Ah, so if the array was properly resized in a later step, this code might execute correctly after all?

 
clemmo: Ah, so if the array was properly resized in a later step, this code might execute correctly after all?

Since the compiler does not have the higher inelegance to analyse and understand every logical path your code might take, it does not check for "array out of range" scenarios during compilation and only during runtime.

 
Very helpful, thank you.
Reason: