0

So I'm working with a dynamically allocated array, and I've set it to hold 5 elements, which should thus be 0-4. I made a function to reserve it if necessary, but I wanted to see if I was getting the program crash I expected when I assigned a value to the array at [5]. But, no error, not until [6].

Here's my code:

int* dynamic_arr;
dynamic_arr = new int[5];

for(int i = 0; i <= 100; i++){
    dynamic_arr[i] = i;
    used++;
    cout << dynamic_arr[i]<<endl;
}

Here's the output:

0 //i[0]
1 //i[1]
2 //i[2]
3 //i[3]
4 //i[4]
5 //i[5]  <-- should be out of range

After that it crashes.

Why am I able to assign a value to a portion of the array that is, well, out of range for lack of a better term?

Thanks

OpSrcFTW

EDIT: Appreciate the answers guys, thanks. I'll read more before posting for a quick answer next time, sorry.

5
  • 3
    UB means undefined behavior! Commented Feb 19, 2013 at 23:33
  • Undefined behavior will kick you when you're not looking. Commented Feb 19, 2013 at 23:35
  • This is indeed interesting. I always expect to see dancing unicorns. It's mildly strange that they don't appear on your machine! Commented Feb 19, 2013 at 23:36
  • Buggy code is much harder to understand than correct code. That's another reason to avoid it. Commented Feb 19, 2013 at 23:39
  • Easy with the downvote guys... Commented Feb 19, 2013 at 23:40

4 Answers 4

5

Accessing beyond the end of an array produces undefined behavior. It does not necessarily produce a crash. Anything can happen.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, also, for your reply. As I said in my response to @juanchopanza , that rings a bell. I should've remembered that. Thanks!
4

Accessing the array beyond its bounds is undefined behaviour. Anything could happen, it need not be repeatable, and there is little point trying to "understand" any patterns you think might be there.

2 Comments

Ok that's fair. For some reason, I had thought it would always crash. But, your statement rings a bell, and it makes sense when I think about what's happening on a lower level. That was a quick response, also! As soon as SO lets me, I'll select this as answer for being first.
It would be hard for typical platforms to make it always crash. Consider if a slightly larger block of memory was being reused. Or consider if there was a slightly larger gap between two blocks of memory already used. And most platforms can only do hardware memory protection at page boundaries, so it would have to line up the access right at the end of a page, which would mean accesses before the beginning couldn't always crash unless the allocation happened to be an exact multiple of a page boundary.
4

Accessing beyond the size of the array will produce undefined behavior. Undefined behavior includes things like your code crashing, working as perfectly fine, working on one computer and not another, restarting your computer or destroying the whole universe.

1 Comment

I hate it when I accidentally destroy the whole universe. It's a b***h to debug...
2

As others have already pointed out, you have undefined behavior, so while a crash is possible, the code can also appear to work.

In a case like yours, appearing to work when you access slightly past the end of the memory you allocated is fairly common. The code that manages the memory for the free store will often round your request size up to the next size it finds convenient, such as a power of two. As such, it's not particularly rare to have at least a little memory past the end of what you requested that you can write to without an visible problems.

You cannot, of course, count on that though -- not even with the same compiler using the same flags, etc. Just for example, the standard library could decide how to act based on the amount of memory available on the target machine, using more padding to optimize speed when there's plenty of RAM available, and less padding to reduce memory usage when there's less available.

As such, no you can't depend on a crash at any particular point -- but this also isn't a place you can get by with testing or thinking it's something you only need to worry about if you're going to port the code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.