0

I allow the user to enter the number of iterations for my array. I am trying to understand what happens When you exceed the max size of 10 and enter a number higher than that, such as 255. The user can then enter in numbers for each iteration. The program allows for a couple extra inputs, but crashes at around 12 or 13. Why is this happening? Why can't the program allow for the 255 iterations specified? I believe it has to do something with the way memory is referenced in c++, but I am not sure.

#include <iostream> 
using namespace std;

int main()
{
int nums[20] = { 0 };
int a[10] = { 0 };

cout << a << endl;
cout << nums << endl;

cout << "How many numbers? (max of 10)" << endl;
cin >> nums[0];

for (int i = 0; i < nums[0]; i++)
{
 cout << "Enter number " << i << endl;
 cin >> a[i];
}

// Output the numbers entered
for (int i = 0; i < 10; i++)
    cout << a[i] << endl;

  return 0;
 }      
}

If this program is run and we enter 255 for how many numbers, and 9 for every single number, then the program does this:

How many numbers? (max of 10)
255
Enter number 0
9
Enter number 1
9
Enter number 2
9
Enter number 3
9
Enter number 4
9
Enter number 5
9
Enter number 6
9
Enter number 7
9
Enter number 8
9
Enter number 9
9
Enter number 10
9
Enter number 11
9
Enter number 12
9
//(program crashes somewhere around here.)
  • undefined behaviour, simple as that. The c++ standard gives you no guarantees about what happens when you do this!! – Paul Rooney Feb 28 '17 at 03:01
  • Your code even specifies that the "how many numbers" shouldn't exceed 10, so presumably you're aware that writing outside the array bounds is bad. Your array is size 10. If you store more than 10 entries, you're invoking undefined behavior. What are you expecting to happen? – ShadowRanger Feb 28 '17 at 03:02

3 Answers3

1

This:

int a[10] = { 0 };
cin >> a[i];

Is only valid if i is between 0 and 9. Any value outside of that is "undefined behavior" which means the program could do anything. Including not crashing for the first couple violations then crashing later. Including crashing immediately. Including never crashing. Or anything else. It's undefined.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

What you are doing in your code is that you are moving outside the valid range of the array a[]. This moves on to undefined behaviour, as you experienced. Even going to a[10] is incorrect, even if your program crashed after you entered the 12th number. The very meaning of undefined behaviour is that the kind of error will differ from machine to machine; another machine may take up to a[16] before crashing, or one may simply, and rightly, stop and crash at a[10]. That is why it is called undefined behaviour; knowing that it will crash sooner or later, but not seeing a fixed pattern.

This undefined behaviour stems from your computer's memory. Everytime you rerun your program, your array is allocated a different block of memory in your local memory. This means that the memory blocks around could be filled or unfilled. As a result, if your code is faulty and you extend an array's index out of its range, it may works for the next few memory bytes if they are empty and accessible. However, as soon as they reach a full byte, or if that memory byte is inaccessible, then it will lead the program to crash. Also, since this array's location in memory can change as the program is rerun, "how far" it will go before crashing will depend entirely on the neighbouring bytes of memory.

This is a lot of theory, so if I got something wrong here by accident, please let me know. For further reading, you can visit Wikipedia

BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31
0

The simple answer to this is that your program is trying to write to bits that you have not allocated. It will do this until it tries to write to a bit that another process is using. This is what is crashing it, because this bit is unavailable. You just appear to be hitting "already in use" around 12 or 13.

パスカル
  • 479
  • 4
  • 13