Hi, if I define an array

for example

int array[2];

How come I can define the following without any problems


Shouldn't it give an error, how come that works for me even though I have originally declared an array which is smaller in size. ????????????????????????????????????
C++ does not do bounds checking on arrays - it's one of the pitfalls for programmers, accessing memory beyond bounds.
Sometimes you are 'lucky' and it all appears to work, other times your program will behave strangely...
Always put your own bounds checking code if using arrays.
To add to the explanation, as I understand it...

When you declare an array of a certain size, the memory is allocated for it as you specify. Then when you try to use an index of the array, the compiler directs the computer to access be beginning of the array and then "step off" the index amount of memory units in contiguous memory. So when you access out of range memory locations using an array index, that memory is really still free for the taking by the program (or might have already been taken by another part of your program, which still needs it!)... That is why things are sure to go haywire when you do that.

ok thankssssssss
Topic archived. No new replies allowed.