## Crash on termination

I've built a program which creates an array and ultimately changes the amount of elements available before performing some mathematical calculations. According to my knowledge I've created a "Dynamic Array", (Seeing as I've changed its size in the code), and have discovered it as the problem.

The problem: The program works entirely as expected, performing effectively until "closed" at "return 0;". It always crashes at termination unless I change the circumstances surrounding the array.

Steps Taken:
Original Code:

Code:
Code:
```#include <iostream>

using namespace std;
int main()
{
const int array_slots = 5;

cout << "This program has created a CONSTANT 'Integer' for an array. This integer contains the value '5'" << endl <<endl;
cout << "This program will now create the array itself, and supply it with 1 element." << endl << endl;
int numberarray[1] = { 0 };
cout << "This program will now use the CONSTANT INTEGER's value to determine a new 'number of elements' for the array it just created, and initialize each one to zero." << endl << endl;
numberarray[array_slots] = { 0 };

cout << "If this program has succeeded, then you will see this message. Below, you should see the number 4, (5 elements, index starting at zero)." << endl << endl;
cout << sizeof(numberarray);

cout << "Please enter a new value for integer/index 0, (Element ONE), in the array: ";
cin >> numberarray[0]; cout << endl << endl;
cout << "Now, enter a value for the remaining 4 elements. Remember, as these are integers they must be numbers." << endl << endl
<< "Press enter after you have entered each element." << endl << endl;

cout << "Element 2: "; cin >> numberarray[1];
cout << "Element 3: "; cin >> numberarray[2];
cout << "Element 4: "; cin >> numberarray[3];
cout << "Element 5: "; cin >> numberarray[4];
cout << endl << endl;

cout << "You should now see the values of multiple problems performed using the elements provided." << endl << endl;
cout << "Index 0 multiplied by Index 3: ";
int comboA;
comboA = numberarray[0] * numberarray[3];
cout << comboA << endl << endl;
cout << "The combination of the above value, minus element 5: "
<< comboA - numberarray[4] << endl << endl;
cout << "Element 3 PLUS index 0: " << numberarray[2] + numberarray[0] << endl << endl;
cout << " The combination of Element 1 multiplied by Element 4, (Seen above ^^^), multiplied by index 4: "
<< comboA * numberarray[4] << endl << endl;
cout << "If this program has reached this line, chances are it has been successful in its attempt to run correctly." << endl << endl;
cout << "Enter any integer value to exit: ";

int programhold = 0;
cin >> programhold;
return 0;
}```
I was attempting to see if I could create, (Declare?), an array before assigning it a quantity of elements. The IDE stated I couldn't create an array with zero elements, ("zero/no index?"), so I chose 1. AKA "int numberarray[0];" didn't work but [1] did.

The program compiled and ran but crashes at termination. I modified the code to create the array with [5] integers without initialization as seen below.

Code:
Code:
```#include <iostream>

using namespace std;
int main()
{
const int array_slots = 5;

cout << "This program has created a CONSTANT 'Integer' for an array. This integer contains the value '5'" << endl <<endl;
cout << "This program will now create the array itself, and supply it with 1 element." << endl << endl;
int numberarray[5];
cout << "This program will now use the CONSTANT INTEGER's value to determine a new 'number of elements' for the array it just created, and initialize each one to zero." << endl << endl;
numberarray[array_slots] = { 0 };

cout << "If this program has succeeded, then you will see this message. Below, you should see the number 4, (5 elements, index starting at zero)." << endl << endl;
cout << sizeof(numberarray);

cout << "Please enter a new value for integer/index 0, (Element ONE), in the array: ";
cin >> numberarray[0]; cout << endl << endl;
cout << "Now, enter a value for the remaining 4 elements. Remember, as these are integers they must be numbers." << endl << endl
<< "Press enter after you have entered each element." << endl << endl;

cout << "Element 2: "; cin >> numberarray[1];
cout << "Element 3: "; cin >> numberarray[2];
cout << "Element 4: "; cin >> numberarray[3];
cout << "Element 5: "; cin >> numberarray[4];
cout << endl << endl;

cout << "You should now see the values of multiple problems performed using the elements provided." << endl << endl;
cout << "Index 0 multiplied by Index 3: ";
int comboA;
comboA = numberarray[0] * numberarray[3];
cout << comboA << endl << endl;
cout << "The combination of the above value, minus element 5: "
<< comboA - numberarray[4] << endl << endl;
cout << "Element 3 PLUS index 0: " << numberarray[2] + numberarray[0] << endl << endl;
cout << " The combination of Element 1 multiplied by Element 4, (Seen above ^^^), multiplied by index 4: "
<< comboA * numberarray[4] << endl << endl;
cout << "If this program has reached this line, chances are it has been successful in its attempt to run correctly." << endl << endl;
cout << "Enter any integer value to exit: ";

int programhold = 0;
cin >> programhold;
return 0;
}```
This didn't crash at termination, everything worked fine. I deduced from this observation that for some reason the program understood and changed the array size from [1] to [5] in the original code, but reverted back to the original value of [1], (Or something other than [5]), prior to termination, causing a crash. I know the size change occurred because the math problems were a success, I know the program is having an issue due to the size change because the crash doesn't occur when the size remains the same throughout.

I also identified that the crash doesn't occur if the "declaration"? of the array is made in global and not within local as seen below:

Code:
Code:
```#include <iostream>

using namespace std;

int numberarray[1] = { 0 };

int main()
{
const int array_slots = 5;

cout << "This program has created a CONSTANT 'Integer' for an array. This integer contains the value '5'" << endl <<endl;
cout << "This program will now create the array itself, and supply it with 1 element." << endl << endl;
cout << "This program will now use the CONSTANT INTEGER's value to determine a new 'number of elements' for the array it just created, and initialize each one to zero." << endl << endl;
numberarray[array_slots] = { 0 };

cout << "If this program has succeeded, then you will see this message. Below, you should see the number 4, (5 elements, index starting at zero)." << endl << endl;
cout << sizeof(numberarray);

cout << "Please enter a new value for integer/index 0, (Element ONE), in the array: ";
cin >> numberarray[0]; cout << endl << endl;
cout << "Now, enter a value for the remaining 4 elements. Remember, as these are integers they must be numbers." << endl << endl
<< "Press enter after you have entered each element." << endl << endl;

cout << "Element 2: "; cin >> numberarray[1];
cout << "Element 3: "; cin >> numberarray[2];
cout << "Element 4: "; cin >> numberarray[3];
cout << "Element 5: "; cin >> numberarray[4];
cout << endl << endl;

cout << "You should now see the values of multiple problems performed using the elements provided." << endl << endl;
cout << "Index 0 multiplied by Index 3: ";
int comboA;
comboA = numberarray[0] * numberarray[3];
cout << comboA << endl << endl;
cout << "The combination of the above value, minus element 5: "
<< comboA - numberarray[4] << endl << endl;
cout << "Element 3 PLUS index 0: " << numberarray[2] + numberarray[0] << endl << endl;
cout << " The combination of Element 1 multiplied by Element 4, (Seen above ^^^), multiplied by index 4: "
<< comboA * numberarray[4] << endl << endl;
cout << "If this program has reached this line, chances are it has been successful in its attempt to run correctly." << endl << endl;
cout << "Enter any integer value to exit: ";

int programhold = 0;
cin >> programhold;
return 0;
}```
This led me to deduce that the size of the array can change, if the array isn't declared locally, supporting my theory that the data stored locally within the programs code is having difficulty distinguishing between the original value and its changed value, ([1] and [5]).

My questions:

1. Why does my program run, but crash upon termination in the original code?
2. Why does moving the declaration to global solve the problem?
3. Is the compiler to blame?
4. Is it possible to keep the declaration of the array and the size change of the array local, while also solving the crash problem.

If you could help me solve this I would be most grateful. My understanding of the situation is preventing me from fully grasping how C++ works.