SRL Archive - C/C++ Help and Tutorials

Arrays resizing


Bad Boy JH

2011-10-01 09:27:47

I know I can strictly not change an array size, and that vectors would be better. However I am looking for simplicity on this small snippet of code in my assignment.

I need to have a variable amount of queues (yes queues as in linked lists, but I of course have to write the damn things), but to be set when the class constructor is called, and set there until it is deleted. How can I do it.

I have

In .h file

private:
int NumberOfQueues;
int UnloadingCapacity;
Queue<Carriage>* Queues;

and in .cpp file


UnloadingJunction::UnloadingJunction(int NumOfQueues, int UnloadCapacity)
{
Queue<Carriage> tmpQueues[NumOfQueues];
Queues = tmpQueues;
NumberOfQueues = NumOfQueues;
UnloadingCapacity = UnloadCapacity;
}

Queue is a template class, Carriage is another class.

I think that works, I want someone to double check it, or suggest a simpler way of doing it.

I don't need to learn vectors, so I really can't be bothered fiddling around with them either.

Brandon

2011-10-01 10:04:10

Well I see your using dynamic arrays already which is good but you can make an array of a larger size, use memcpy to copy the old array's contents to an array with a new size; delete the old array!

Thats a way of sorta resizing a dynamic array.

Check out this: http://www.daniweb.com/software-development/cpp/threads/245637/1075107#post1075107

Wizzup?

2011-10-01 10:25:31

From: ggzz
Well I see your using dynamic arrays already which is good but you can make an array of a larger size, use memcpy to copy the old array's contents to an array with a new size; delete the old array!

Thats a way of sorta resizing a dynamic array.

Check out this: http://www.daniweb.com/software-development/cpp/threads/245637/1075107#post1075107


Or you can use realloc?