dynamic memory

Sep 12, 2014 at 12:02am
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;

}
Sep 12, 2014 at 12:36am
You need to initialize the variable size with a value.
 
int size = 5; //any integer value 
Last edited on Sep 12, 2014 at 12:38am
Sep 12, 2014 at 12:38am
Isn't the point of dynamic memory allocation to allocate memory during run time?
Sep 12, 2014 at 12:38am
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.
Sep 12, 2014 at 12:50am
Thank you. I got it.
Topic archived. No new replies allowed.