2

According to the correct answer in Static array vs. dynamic array in C++ static arrays have fixed sizes.

However, this compiles and runs just fine:

int main(int argc, char** argv) {

int myArray[2];

myArray[0] = 0;
myArray[1] = 1;

cout<<myArray[0]<<endl;
cout<<myArray[1]<<endl;

myArray[4];

myArray[2] = 2;
myArray[3] = 3;

cout<<myArray[2]<<endl;
cout<<myArray[3]<<endl;

return 0;
}

Does this mean a static array can be resized?

Community
  • 1
  • 1
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
  • No, by accident it doesn't fail. – GolezTrol Nov 18 '12 at 12:02
  • It depends on what you use. Visual studios recognizes it is an error and i get `Run-Time Check Failure #2 - Stack around the variable 'myArray' was corrupted.` but more importantly dasblinkenlight said what i was going to say. Its a dummy read on myArray[4] it isnt a declaration/resize –  Nov 18 '12 at 12:05
  • @acidzombie24 cool - VS uses stack protection implicitly. – John Dvorak Nov 18 '12 at 12:06
  • fyi learning how to use stack/deque/map/set is a good idea –  Nov 18 '12 at 12:10

3 Answers3

7

No, not a chance in hell. All you've done is illegally access it outside it's bounds. The fact that this happens to not throw an error for you is utterly irrelevant. It is thoroughly UB.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • My best guess is that it should crash upon return from `main` on most machines including all `x86`s – John Dvorak Nov 18 '12 at 12:05
  • @KorayTugay Undefined behavior. Which means anything can happen and the compile doesnt need to report errors about it –  Nov 18 '12 at 12:06
  • 2
    @KorayTugay undefined behavior. The machine catching fire cannot be ruled out. – John Dvorak Nov 18 '12 at 12:07
7

You're not actually enlarging the array. Let's see your code in detail:

int myArray[2];

myArray[0] = 0;
myArray[1] = 1;

You create an array of two positions, with indexes from 0 to 1. So far, so good.

myArray[4];

You're accessing the fifth element in the array (an element which surely does not exist in the array). This is undefined behaviour: anything can happen. You're not doing anything with that element, but that is not important.

myArray[2] = 2;
myArray[3] = 3;

Now you are accessing elements three and four, and changing their values. Again, this is undefined behaviour. You are changing memory locations near to the created array, but "nothing else". The array remains the same.

Actually, you could check the size of the array by doing:

std::cout << sizeof( myArray ) / sizeof( int ) << std::endl;

You'll check that the size of the array has not changed. BTW, this trick works in the same function in which the array is declared, as soon you pass it around it decays into a pointer.

In C++, the boundaries of arrays are not checked. You did not receive any error or warning mainly because of that. But again, accessing elements beyond the array limit is undefined behaviour. Undefined behaviour means that it is an error that may be won't show up immediately (something that is apparently good, but is actually not). Even the program can apparently end without problems.

Baltasarq
  • 12,014
  • 3
  • 38
  • 57
5

First, this is not a static array, it is an array allocated in the automatic storage.

Next, the

myArray[4];

is not a new declaration, it is a discarded read from element #4 of the previously declared 2-element array - an undefined behavior.

Assignments that follow

myArray[2] = 2;
myArray[3] = 3;

write to the memory that is not allocated to your program - an undefined behavior as well.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523