output multiprecision integer while converting to desired base

39 Views Asked by At

I want to write a method (in C++) to output a multiprecision integer x in base b. The usual way is to divide x by b, store the reminder d, recursively apply this to the quotient, then output d.

Problem: I need space to store the quotient. I don't want to overwrite the original number x, nor use a static array (not thread-safe), and don't want to allocate and free heap space with every call to the method, if avoidable.

An idea is to use an automatic array within one method (occupying stack space) for the quotient, then call the recursive method which overwrites the quotient in every recursion step with the new quotient, thus using only linear stack space. But variable length arrays are not portable.

0

There are 0 best solutions below