New Programmer_C++_Possible Problem with one-dimensional arrays(Dynamic?)

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.
There are a few problems here.
An array of 1 element is declared.
This line:
numberarray[array_slots] = { 0 };
does not modify the size of the array.
It actually assigns a new value to the element located at subscript array_slots

Since the array has just one element, the only valid subscript is zero, such as
numberarray[0] = 12345;

This line cout << sizeof(numberarray); outputs the value 4, which is the same as cout << sizeof(int);, that is, the number of bytes occupied by an integer. Thus it shows that the array has just one element.

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
#include <iostream>

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

    int * numberarray = NULL; // Pointer, initialise to NULL
	
    cout << "This program will now use the VARIABLE value to determine "
            "a new 'number of elements' for the array." << endl << endl;

    numberarray = new int[array_slots]; // request memory for array_slots integers 

    cout << "If this program has succeeded, "
            "Below, you should see an address which is non-zero." << endl << endl;
    cout << numberarray;



    // When we are finished, release the memory used by the array
    
    delete [] numberarray;

    int programhold = 0;
    cin >> programhold;
    return 0;
}


Note that int array_slots does not have to be a constant in this case. For example it could be some value derived at run-time, such as a number entered by the user at the keyboard.

After the pointer has been given a valid address by the new [] operator, it can be used just like an ordinary array.
Last edited on

1. Why does my program run, but crash upon termination in the original code?
Because you have modified the contents of areas of memory outside your array. Results are unpredictable.
2. Why does moving the declaration to global solve the problem?
It doesn't solve it. It merely happened to disguise the error. But that was by no means guaranteed.
3. Is the compiler to blame?
Not really. Though some compilers might warn of invalid syntax.
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.
Yes. Use new and delete to allocate and release memory.

http://www.cplusplus.com/doc/tutorial/dynamic/
Last edited on
Topic archived. No new replies allowed.