-1

I am having problems with arrays it shows counting up to 127 when I execute the following code:

#include<iostream>
using namespace std;
int main()
{
    int x[5];
    int z=0;
    while(1)
    {
        x[z]=z;
        cout<<x[z];
        z++;
    }
}

My problems

  1. If I had given the maximum indexes of array 'x[5]', then why it is not throwing an exception on exceeding the limit.
  2. If it is exceeding, then it is stoped on the array's index 127 'x[127]'.

I'm using dev-c++ with TDM-GCC 4.9.2 64-bit Compiler.

  • 3
    There are no exceptions that will be thrown... And even if it was you aren't catching any either.. You can look into throwing your own by throwing: std::out_of_range. These are only thrown for std data structures (i.e. std::vector, etc.) – Omid CompSCI Nov 18 '19 at 04:43
  • C just dont have an implicit bound checking feature. And why it stopped at 127 is probably that your operating system detected an invaild memory access. – KagurazakaKotori Nov 18 '19 at 04:44
  • 1
    Possible duplicate of [Array index out of bound in C](https://stackoverflow.com/questions/671703/array-index-out-of-bound-in-c) – Aykhan Hagverdili Nov 18 '19 at 05:03
  • Look for the more information on array out of bound https://stackoverflow.com/questions/15646973/how-dangerous-is-it-to-access-an-array-out-of-bounds – TruthSeeker Nov 18 '19 at 05:19
  • Possible duplicate of [How dangerous is it to access an array out of bounds?](https://stackoverflow.com/questions/15646973/how-dangerous-is-it-to-access-an-array-out-of-bounds) – TruthSeeker Nov 18 '19 at 06:12

1 Answers1

1

Because it's not a guaranteed memory exception when going out of bounds, it's merely undefined behavior. In your case it's a local variable so it's going to consistently walk through the stack until some undefined event either crashes or ends the program.

Pickle Rick
  • 808
  • 3
  • 6
  • But why only 127 on Dev-Cpp. however, if I do write the same code using CodeBlocks. It shows the counting up to 73. – Muhammad Hassan Shafique Nov 19 '19 at 18:25
  • While when I globally declaring the array with index 5. In Dev-Cpp, it shows counting up to 2543 In codeBlocks, it shows counting up to 277. – Muhammad Hassan Shafique Nov 19 '19 at 18:27
  • Well undefined behavior is exactly that, you can't know exactly how it's going to break things. In your case it's almost certainly all for the same reasons though. First two examples are walking up the stack (which grows downwards) and the exception hits based on how far from the top of the stack you are. Once reaching the top of the stack it will then move into a separate memory page which is likely invalid memory. – Pickle Rick Nov 19 '19 at 19:26
  • When defining it as a global it's the same explanation but you're moving through the .data section in your process. Once you get past the rest of the image in memory it's likely the next page is inaccessible. – Pickle Rick Nov 19 '19 at 19:29
  • Also note you're dealing with an array, so that index should be multiplied by 4 to see how far you've moved in memory. Your example using a global would be ``10,172`` bytes which sounds about right for a basic ``main`` image. – Pickle Rick Nov 19 '19 at 19:32