Dynamically changing array size?

I prefer to include as few headers as I can, and when dynamically allocating memory, is there a way to do something like this?
1
2
3
4
5
6
7
int index=1;
int *array=new int[index];
do{
cout<<"Enter a number to add: ";
cin>>array[index-1];
index++;
}while (array[index-1]!=0);


My goal is to change the size of the array and simultaneously add to it. Is there a way to do that?
I prefer to include as few headers as I can

Any particular reason? ;)
And I assume therefore that you'll reject the suggestion of <vector>. I don't see why as the C Standard algorithms are likely to be more robust and efficient than anything you or I could write in a reasonable time.
The only way to create arrays that have a size that can be dynamically changed is with vectors. Read into the STL (Standard Template Library), theres some good stuff there.
Last edited on
Alright.
Any particular reason? ;)

Not really. I think it has to do with not understanding the source code of them, as I'm just getting to polymorphism in C++.

Thanks, anyway though =)
Last edited on
The only way to create arrays that have a size that can be dynamically changed is with vectors.
Er, do you know how the STL vector works?

Technically (according to the C++ standard), you can't change the size any array, but (according to the OS) you may request a resize or reallocate dynamically allocated memory (which C++ can treat like an unbounded array).

The C dynamic memory functions provide for resize/reallocate requests, but C++ does not directly. You should reallocate something like this:

1
2
3
4
5
6
{
  int *newarray = new int[newsize];
  std::copy( array, array+std::min(oldsize,newsize), newarray );
  delete [] array;
  array = newarray;
}

You may notice that this is, technically, not a reallocation, but a relocation. That is, it creates a new array and copies the data from the old over to the new, and destroys the old.

The STL vector class does just this, with one more optimization: it allocates more than is necessary so that there is room to grow before having to 'reallocate', and simply keeps track of exactly how much of the array is currently used.

Hope this helps.
Excuse me, you have a good point, I worded that rather stupidly. You can't change the size of an array, but a vector will always have enough room for more elements because of the optimization that you pointed out. I don't use vectors very often, or C++ for that matter so I do say some stupid stuff :/
You aren't stupid. Just be careful about making blanket statements. (Programmers are notoriously picky about exactness in language. ;-)
Huh. This code compiles and runs perfectly:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
using namespace std;

int main(){
	int index=1,
		*array=new int[index],
		tot=0;
	
	do{
		cout<<"Enter a number to add: ";
		cin>>array[index];
		index++;
	}while (array[index-1]!=0);
	
	for (int i=0;i<index;i++){
	        tot+=array[i];
	}
	
	cout<<"Total: "<<tot<<endl;
	return 0;
}

Any ideas on why this works? I was just playing around with it for a while.
It works because the universe likes you right now. Give it a short bit to get back to normal...
Topic archived. No new replies allowed.