Dynamically increase array

I almost posted this in beginners, sorry if it's wrong...

If been looking at a solution all day. The problem is as follows: I want to send network messages using arrays. I'm making a vector-like class that automatically behaves the way I want (deep copies). Obviously, I don't want the array too big, or else the heap will overflow. I want it big enough to hold my data, so my demands are that the array can increase about like how vector does. This all works fine until the array is filled.

Problem is I don't know how to call additional memory for my array. Delete doesn't work; I've commented it out and the *data=*temp also fails. I've tried realloc with no success. I can't quite follow the stl's vector code. How can I solve this problem? Thanks all.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const int MAXELEMENTS=16;

template <class T>
class LeetVector{
public:
	LeetVector():numElements(0),capacity(MAXELEMENTS){}
	void push_back(const T& _Right){
		if(numElements>capacity-1){
			capacity*=3;
			T *temp=new T[capacity];
			memcpy(temp, data, (size_t)(capacity/3));
			delete[] data;
			*data=*temp;
		}
		data[numElements++]=_Right;
	}
...
private:
	unsigned int capacity;
	unsigned int numElements;
	T data[MAXELEMENTS];
};
The STL List (http://www.sgi.com/tech/stl/List.html) isn't suitable for you?

The best way for you to achieve what you want (in my opinion) is probably through a linked-list of double-linked list. This way you'd only be allocating memory as required, de-allocating on erase/delete/remove and it would grow/shrink with no problem.

Edit: The other way you could do it is with an array of pointers that is created dynamically. When the array is filled, you'd need to create a new temp array to move them into that has more space and point your array at that. More work than a LL or DLL (linked-list)
Last edited on
You mentioned
"point your array at that"
I think that's just the part I need, I've done the other stuff. How would one go about that?
1
2
3
char *newarray = new char[XX];
// Copy OldArray contents into new Array
oldarray = newarray;
When I tried that I got "cannot convert from 'T *' to 'T [16]'" where T is the class being converted. It repeats that for every class I tried the new vector.
Your problem is that you are creating T an an array of a static size. You cannot do that.

You'd need to declare it as a pointer. T[X] will always be X. There isn't any way to change the size of it. Especially if it's a class object, you'd have to re-declare it. Or create a new copy of the entire class with the new size.
Last edited on
Hmph okay. I'll just go back to my original problem.
I'll try and simplify my explanation.

You have a class, and inside that class you have a member variable. When the class is instantiated (created) that variable is created with a static size. Once that class exists, you cannot change the size of that variable because it was created through a static instantiation.

The way to get around this is to use dynamic instantiation. You would declare your class variable as T *pData. Then in your constructor you would have pData = new T[NUM_ELEMENTS];. This means that the size of it is still created the same, no need for const integers etc. When you need to change the size of it, you can create a new temp array of a bigger size, copy the objects over and then point pData at the new temp array as I have shown you.


From a theoretical point of view, you have designed your class as a wrapper around T data[NUM_ELEMENTS]. You really need to design your class not as a wrapper, but as a manager of an array. You array should've been declared as a pointer the class can manage (and inherently, provide access to through Wrapper functions). You have gone for a straight-forward wrapper approach with no management capabilities.

Sorry, I have tried to simplify my explanation as much as possible.


I understand. I've already implemented that stuff before, the result being an stl vector, pretty much. I thought I could cheat this way. I did consciously want a wrapper for a T arr, but apparently I can't. I'll have to find a different way. Thanks for the info+help.
Last edited on
vector isn't the only STL container. A list, stack, queue all work equally well and have different advantages/dis-advantages.
Topic archived. No new replies allowed.