0

So i have an array called digits that has dynamic allocated memory. The initial capacity is set to a default capacity of 20. I'm trying to figure out how to implement the following code so that if something is added to the array that exceeds the capacity the code will create a new array that is 2^n bigger (i.e. 40, 80, 160). however i want it to have a for loop that will make the array 2^n bigger until the new capacity is larger then what was entered.

void BigNum::resize(size_t n)
{
size_t *NEW_CAPACITY;

if(n == capacity)
    return; // The allocated memory is already the right size

if(n < used)
    n = used;

NEW_CAPACITY = new size_t[n];
copy(digits, digits + used, NEW_CAPACITY);
delete[] digits;
    digits = NEW_CAPACITY;
capacity = n;

Any help is much appreciated

Sean
  • 857
  • 1
  • 11
  • 21
  • 4
    why are you not using `std::vector` – Cheers and hth. - Alf Sep 21 '11 at 06:47
  • 4
    Any specific reason why you don't use a `std::vector`, that does things like that automatically? – nabulke Sep 21 '11 at 06:48
  • cause i have to go about it in this way its a requirement. what i have written is basically what I'm trying to do I just need a for loop that adjusts the NEW_CAPACITY array till it is larger then what is entered. and i need to figure out how to make the array size 2^n bigger on each loop – Sean Sep 21 '11 at 06:52

2 Answers2

1

If you're writing C code using C++ syntax - then use the C methods: realloc and malloc.

If you're writing C++ code - use std::vector instead of arrays.

The loop you're referring to is a simple calculation:

while(current<required) {
    current = 2*current;
}
new_digits = realloc(digits, current);
// check that the allocation succeeded, handle errors, no need
// to copy data - realloc does that for you.

done.

littleadv
  • 20,100
  • 2
  • 36
  • 50
  • i have to use arrays its part of a project. what i am trying to figure out is for dealing with huge numbers if each part of the array corresponds to one number of the larger number and that larger number exceeds the default capacity of 20. Then the array should be resized to 2^n of the default until the new capacity is larger then the big number – Sean Sep 21 '11 at 07:04
  • Why do you call `realloc` inside loop? Also, the way you wrote it makes it impossible to recover from failure in `realloc` (if `realloc` fails, the old value of `digits` is lost and you get a leak). – Idelic Sep 21 '11 at 07:50
  • @Idelic - although I usually dismiss pedantic comments like this, seems to me that the OP is clueless enough to just copy my example, so I'll correct it. – littleadv Sep 21 '11 at 08:18
  • @Sean, if one sentence takes 3 lines, chances are no-one will understand what you're trying to say. – littleadv Sep 21 '11 at 08:19
0

"I'm trying to figure out how to implement the following code so that if something is added to the array that exceeds the capacity the code will create a new array that is 2^n bigger (i.e. 40, 80, 160). however i want it to have a for loop that will make the array 2^n bigger until the new capacity is larger then what was entered."

First, I think you mean 2*n rather than 2^n.

Second, why not use an std::vector like the other commenters said?

Third, I don't fully understand your question but it sounds like you want this:

int newCapacity = capacity;
while( newCapacity < n )
  newCapacity *= 2;

That seems pretty trivial though, so can you clarify your question?

Anson
  • 2,654
  • 21
  • 18