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

3
  • 4
    why are you not using std::vector Commented Sep 21, 2011 at 6:47
  • 4
    Any specific reason why you don't use a std::vector, that does things like that automatically? Commented Sep 21, 2011 at 6: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 Commented Sep 21, 2011 at 6:52

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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
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 - 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.
@Sean, if one sentence takes 3 lines, chances are no-one will understand what you're trying to say.
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.