Weird Array problem?

I was experimenting with arrays and I stumbled upon a weird situation. My program stops running when a specific number is surpassed, I am not sure if it's due to my computer hardware or maybe a bad code. If you have any thoughts on why my program crashes I would appreciate the hint's and help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
int printArray(int a[],int b){
	for(int i=0;i<=b;i++)
	{
	cout<<i<<" = "<<a[i]<<endl;
	}
}
int main()
{
 	int x;
 	int a[x];
 	cin>>x;//If I input a number higher then 4156 my program crashes, any idea why? 
 	printArray(a,x);
 	
 	system("pause");
}
You have invalid code in three places. Firstly subscriptors of an array are in range 0 - N-1, where N - is the namber of array elements. So in your function
printArray the control statement of the loop shall be set as

for (int i=0; i < b; i++ )

Secondly, you shall enter a value for each element of the array. The following statement cin>>x is incorrect.

And at last size of array shall be a constant expression. So you definition of the array also is incorrect

1
2
 	int x;
 	int a[x];


Shall be

1
2
 	const int x = 10; // or other value
 	int a[x];
There fore there is no way to be able to manipulate a Array without giving it a default value before execution?

And about

for(int i=0; i < b; i++)

was I printing out a array element that did not exist due to Arrays law of starting in element 0 not 1?
I think you need to reread about arrays. http://www.cplusplus.com/doc/tutorial/arrays/

"The elements field within brackets [] which represents the number of elements the array is going to hold, must be a constant value, since arrays are blocks of non-dynamic memory whose size must be determined before execution. In order to create arrays with a variable length dynamic memory is needed, which is explained later in these tutorials."

Maybe about simple sequences as well.

You have a nonconforming compiler mixed with undefined behavior.

Line 12 shouldn't be a legal statement. You can only declare a static array with a constant value.

Next up, step through that program yourself, line by line. What size does a start as? If you answered whatever the user enters, you're wrong. You declare and size the array before you get user input.

What actually happens? The size of the array at the start is dependent on how your compiler implements it, it could choose to give uninitialized variables a default value of 0, or it can just as well leave whatever is in memory there, and not touch it at all, x is equal to an unknown amount depending on what was last using that memory location.

At either rate, if it's 0, you step out of bounds immediately, and you need to go forward 4156 * sizeof(int) bytes before you hit critical memory and crash the program, or you declare an amount of memory that is less then 4156 * sizeof(int) bytes, and still step out of bounds and crash the program.
Last edited on
Thank you very much. Ill reread The Array tutorial.

P.S. I am currently using DEV C++ compiler, anything you would recommend? book's? compiler's? video's? or anything in general for a programmers future?
If you want to give a variable x for the array length(what you have tried to do here) try applying "Dynamic Arrays"
Topic archived. No new replies allowed.