Dynamic memory question

Hey.
I'm trying to write a program, which is supposed to hold pointers to variables. Now, it should hold an undetermined amount of pointers.
I could declare a huge 1000 spaces array but I would like to use dynamic memory.
What I need to do is, each time there's an input from the user, it should add one more place for a pointer to my array. At first the user can request say 5 places. Then, further during runtime, the user may need to add a few more spaces, and so, the array should expand as the program goes.
I generally know how to use the new operator. But, my question is: can I not only create, but add more and more spaces to the same pointer as the program goes along? If so, how would I do that?

Thank you.

Something similar to that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main() {
	int num= 1;
	int * myar;
	int i,count=0;

	while (num!= 0) {
	cin >> num;
	myar = new int;
	myar[count] = num;
	++count;
	}

	for (i=0;i<count;++i) {
	cout << myar[i] << endl;
	}

	system("pause");
return 1;
}


The program accept integers from the user, and will keep looping and accepting them until the user hits 0, and then it's supposed to display everything it stored. So, the array has to grow as the program runs.
Last edited on
You can not grow an array. You have to create a new one with the desired size, copy the content of the old one in it, make your pointer point to the new array and deallocate the old array.
Topic archived. No new replies allowed.