How do you grow a circular array?

I have a front, I have a back, I have a current_size, I have a maximum_size, and of course I have the array. So, how do you grow a circular array?

Thank you kindly!

## All 9 Replies

The easiest way, allocate a new larger memory block(array) and then copy the array into it.

>>So, how do you grow a circular array?

You don't. The array itself never changes size. When the current pointer reaches the end of the array then just reset the current pointer back to the beginning of the array -- hence its name is "circular".

my homework assignment is to make a circular array and grow it. I have to grow the circular array. Unfortunately, it is more complex than just making a temporary array, moving everything into there, and continuing. I just don't know how to do it :(

It might be helpful if you posted the text of the assignment instead of trying to paraphrase it.

ok - "You have to use a circular array for queue but it will not be of a set size. You have to allocate space if the capacity gets exhausted."

IMO that defeats the purpose of a circular array. But anyway, just make it a pointer and allocate new memory when needed, similar to what gerard mentioned previously. When the current pointer reaches the current maximum size then allocate a new array of larger size, copy the contents of the current array into the new array, then delete the current array. Not difficult to do once you understand the algorithm.

Once you get that part working then you will have to be concerned about reads from the circular array. You will have to have two temp pointers, one for reading and the other for writing. If the data is not read at least as fast as its written, then the array will eventually fill up and there will not be any more room for writing without overwriting existing data that has not been read. This situation can happen anywhere withing the array, not just at the extreme end of the array.

Thank you; however, I already got it working without the use of any pointers. I just need help with the grow part. How would I implement it using back, current_size, maximum_size, and the array.

I have tried just making a temporary array, storing the contents from the old array into the new array, and then going from there. Yet, the problem with the circular array is that it does not remove the values that were popped off of the queue. So, if those values will still transfer over to the new array, but I don't want them there.

When I said pointers I also meant the items you have already coded. You can code them as either pointers or integers. In any event the array itself will have to be a pointer so that you can allocate it with new operator. For now don't worry about coping old data that has already been read. It will eventually get overwritten with new incoming data so copying old data is not really a problem. You just have to adjust the pointers (integers) to accommodate the array's new increased size.

At this point we really can't help you much more without seeing the code you have already written. There are just too many different ways to do it.

ok, thank you so much. I will go to my professor tomorrow and if he can't help then I will put some code up (:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.