-4

When running this program, if the value of n is set to 10 it will print the indices of array a and stored values from a[0] to a[9]. However if I set the value of n to more than 10 the then it only prints the indices of array a from a[0] to a[5] with their stored values. Can someone explain to me why this is happening?

#include <iostream>
using namespace std;

int main()
{
    int a[10];
    int i, n=11;

    for(i=0; i<n; i++) {
        a[i]= 5;
    }

    cout<<"The array is: \n";   

    for(i=0; i<n; i++)
    {
        cout<<"a["<<i<<"] = "<<a[i]<<endl;
    }

    return 0;
}
21koizyd
  • 1,843
  • 12
  • 25
Surovi Mou
  • 11
  • 3
  • @PaulR the code postoed is a complete example that exhibits the problem. . . on whatever compiler the OP is using. But the problem is clear from the code. – iheanyi Jun 29 '17 at 15:27
  • since you're using C++14 have a look at the range-based for and std::array. Latter may provide additional error handling in debug mode when accessing out-of-bounds indices. – The Techel Jun 29 '17 at 15:27
  • @PaulR Oh, I see, you're being pendantic. Even though the question says the problem exhibits when you change n from 10 to a value greater than 10, and it's a trivial change to make/read, you want the posted code to be exactly the version that triggers a problem. . . here you go: https://ideone.com/sqXos6 – iheanyi Jun 29 '17 at 15:31
  • @PaulR The question is very clear. Code works with n <= 10, blows up with n > 10. I've made an edit that should satisfy you. Edit: Knowing you may nitpick, n <= 10 and n >= 0. – iheanyi Jun 29 '17 at 15:34
  • @iheanyi: thank you for helping to improve the question. I've just cleaned up the text now also, in order to make it easier for others to follow. – Paul R Jun 29 '17 at 15:36
  • Probably it depend IDE, I test your solution and I see all numbers in array, of course programm is broken when is 11 element. (VS) – 21koizyd Jun 29 '17 at 15:44
  • Many duplicates, e.g.: [C++ read/write to array out of bounds](https://stackoverflow.com/questions/38558816/c-read-write-to-array-out-of-bounds), [I Am Able To Go Outside Array Bounds](https://stackoverflow.com/questions/32110799/i-am-able-to-go-outside-array-bounds), https://stackoverflow.com/questions/20984544/will-index-out-of-array-bounds-throw-exception-or-error-before-core-in-c, https://stackoverflow.com/questions/1239938/accessing-an-array-out-of-bounds-gives-no-error-why, https://stackoverflow.com/questions/25204819/how-are-array-bounds-managed-in-c... – Paul R Jun 29 '17 at 17:05
  • Undefined Behaviour is undefined, and `n>10` causes Undefined Behaviour (by accessing `a[i]` when `i≥n`). So anything can happen, including the observed behaviour. – Toby Speight Jul 11 '17 at 16:18
  • Possible duplicate of [Accessing an array out of bounds gives no error, why?](https://stackoverflow.com/questions/1239938/accessing-an-array-out-of-bounds-gives-no-error-why) – Toby Speight Jul 11 '17 at 16:19

1 Answers1

4

If you increase the value of n past the size of the array (or equal to it indexing from 0), you are going past the end of the array. And going past the end of the array is undefined behavior. If your program is exhibiting undefined behavior. Anything can happen. See this blog post from Microsoft for more on undefined behavior

If you switch to an std::array instead of a C array and use .at(), then something well defined will happen, you will get an std::out_of_range exception. For more see http://en.cppreference.com/w/cpp/container/array/at and http://en.cppreference.com/w/cpp/container/array

Curious
  • 20,870
  • 8
  • 61
  • 146
  • so there is no way to tell what will happen if it's past end of the array? Am i right? – Surovi Mou Jun 29 '17 at 15:30
  • @SuroviMou in most cases no, not with `std::array` and `operator[]` at least. If you switch to `.at()` instead of `operator[]` then something well defined will happen. You will get an `std::out_of_range` exception – Curious Jun 29 '17 at 15:31
  • @SuroviMou that's correct. For example, using the ideone and n = 15, this ends up printing a large number of values: https://ideone.com/sqXos6 – iheanyi Jun 29 '17 at 15:32
  • @SuroviMou _"so there is no way to tell what will happen if it's past end of the array? Am i right?"_ No, you aren't right. In your case you could use `sizeof(a)` just to tell you what is valid or not. – πάντα ῥεῖ Jun 29 '17 at 15:35
  • 1
    @πάνταῥεῖ You're wrong, using sizeof(a) won't tell you what would happen should you index past the end of the array, that's literally undefined behavior. – iheanyi Jun 29 '17 at 15:37
  • 2
    @iheanyi I said you can easily use to check if it will be valid or not to access a specific index. – πάντα ῥεῖ Jun 29 '17 at 15:40
  • 2
    You are both right :) Just saying different things! – Curious Jun 29 '17 at 15:42
  • @πάνταῥεῖ Your comment literally does not say that. It quotes a statement, then presumably responds to said statement. It reads like you are contradicting the OP's statement which is correct - what will happen when you index off the end of an stay is undefined. – iheanyi Jun 29 '17 at 15:48
  • 2
    @iheanyi I don't care a lot. Have a cookie: You're right _undefined behavior is undefined_. – πάντα ῥεῖ Jun 29 '17 at 15:49
  • 2
    @πάνταῥεῖ thanks, cookies are delicious – iheanyi Jun 29 '17 at 15:52