I've built a program which creates an array and ultimately changes the amount of elements available before performing some mathematical calculations.
The problem: The program works entirely, performing effectively until "closed" at "return 0;". It always crashes at termination unless I change the circumstances surrounding the array.
Steps Taken:
Original Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#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 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#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.
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.