0

From what I have read, arrays can only be a fixed size, which is defined when you create it (and if you want to create something similar with a dynamic size, you use vectors instead).

However, when I try to set values outside of the array, I am perfectly able to:

int badNums[5] = {4, 13, 14, 24, 34};
badNums[999] = -127;
std::cout << badNums[999] << std::endl;
// Returns -127

And it gets even more bizarre if I try to access values of the array outside of the fixed range:

std::cout << badNums[997] << std::endl;
// Returns 825245046 (but seems to be randomly chosen every time I run it)

What is going on here?

IQAndreas
  • 8,060
  • 8
  • 39
  • 74
  • 2
    Undefined behavior is going on here. No more, no less. – AnT stands with Russia Jun 03 '15 at 21:25
  • @TartanLlama That question was closed as _"not a real question"_, and doesn't actually explain what is going on, or why no errors are being thrown. – IQAndreas Jun 03 '15 at 21:26
  • 2
    @AnT _"Toto, I've a feeling we're not in Java anymore."_ – IQAndreas Jun 03 '15 at 21:29
  • 3
    *From what I have read, I can't cross the street when the light is red, but when I tried it it worked and I ended up on the other side of the street.* -- There's no guarantee that you'll be prevented from doing it, but next time, you might get a ticket or be hit by a car – Keith Thompson Jun 03 '15 at 21:41
  • @KeithThompson I come from a world where whenever you stick your foot outside the sidewalk, an omniscient voice echoes from the sky: _"Hold on there my son; `ArrayIndexOutOfBoundsException`"_ – IQAndreas Jun 03 '15 at 22:08
  • @KeithThompson that's a great analogy, hope I can remember it next time I need it. – Mark Ransom Jun 04 '15 at 02:56

4 Answers4

3

C++ doesn't perform bounds checking, because that would slow things down. It won't stop you from using array indices that are outside of the array (even negative ones!) but once you do you get undefined behavior.

Sometimes undefined behavior appears to work properly. Don't be fooled though, there may be bad things going on behind the scenes that you can't see. Take this slight modification of your example:

int badNums[5] = {4, 13, 14, 24, 34};
int sentinel = 0;
badNums[6] = -127;
std::cout << badNums[6] << sentinel << std::endl;

On many compilers you'll see that sentinel has changed value to -127, although since this is undefined behavior it is not guaranteed.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
1

In short

you are reading the memory that is laying behind the array. That is 'perfectly fine' as long that memory belongs to you(r process).

Long(er) Answer

When you create an array, you are reserving a certain space of memory (in your case 5*sizeof(int) (should be ~40byte)). When you try to access data that is out of bounds (that's the corrent term for that what you are doing), everything could happen. You are lucky and you are accessing memory, that belongs to your process, otherwise the OS should (and will) kill your process and you will get a SEGFAULT.

Always be certain, that you are reading and writing inside of your array bounds. Everything else could lead to bad runtime behavoir.

Bonus

To check, if you are accessing out of bounds, you could use valgrind

valgrind ./yourProgram

and then valgrind will print every access, that should not be done.

hellow
  • 12,430
  • 7
  • 56
  • 79
0

You're setting a location in memory to that value, however you haven't told the computer to reserve that location for your purposes. It could get overwritten by a different process, or another variable you define.

As other people have said, there can also be unpredictable behavior if that memory is already reserved, say by another program.

FreshWaterTaffy
  • 270
  • 1
  • 2
  • 18
0

badNums[997] is identical to *((badNums)+(997))
You're trying to access some memory where the content could be anything.
Could be an old value used some other program, could be a value used by your program.
This results in undefined behaviour.

Andreas DM
  • 10,685
  • 6
  • 35
  • 62