dynamic memory

I am getting an error or uninitialized local variable 'size' used and i don't know why. Can someone explain it to me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 #include <iostream>
using namespace std;

int main()
{

	int *arry; 
	int size;
	arry = new int [ size ];

	cout << "How big is the array" << endl;
	cin >> size;
	return 0;

}
You need to initialize the variable size with a value.
 
int size = 5; //any integer value 
Last edited on
Isn't the point of dynamic memory allocation to allocate memory during run time?
Code executes in order.

1
2
3
	int *arry;  // <- this declares a pointer
	int size;  // <- this declares a variable
	arry = new int [ size ]; // <- this allocates an array... but of what size?  You never say 


You don't get the size from the user until line 12... which is after you have already created the array on line 9.
Thank you. I got it.
Topic archived. No new replies allowed.