0

I am attempting to create a Big Integer class but have run into an issue. I have an overloaded constructor that's giving me some issues. It is supposed to take in a string and convert that string into a std::uint8_t* called m_number, keep track of the number of digits, and keep track of space allocated for each std::uint8_t*. The numbers are loaded in backwards, with the least significant digit being m_number[0]. The issue I'm having is with storing '0' into the array. The function works fine for any other value, but if a string has a '0' in it, the function stops storing values into the std::uint_8*. Here is my class definition:

      class BigInteger
    {
    public:
        BigInteger add(const BigInteger& rhs);
        BigInteger multiply(const BigInteger& rhs);
        void display();
        BigInteger();
        BigInteger(const BigInteger& rOther);
        BigInteger(int);
        BigInteger(std::string);
        BigInteger& operator=(const BigInteger & rhs);

    private:
        std::uint8_t* m_number;
        unsigned int m_sizeReserved;
        unsigned int m_digitCount;
}

Here's my overloaded constructor:

BigInteger::BigInteger(std::string str) {
    m_digitCount = str.length();
    m_sizeReserved = m_digitCount;
    m_number = new std::uint8_t[m_sizeReserved];
    std::uint8_t* aArray = new std::uint8_t[m_sizeReserved];
    int j = str.length()-1;
    for(int i=0; i < m_digitCount; i++, j--) {
            aArray[i] = str[j]-'0';
    }
    for(int i=0; i < m_digitCount; i++) {
        m_number[i] = aArray[i];
    }
    delete[] aArray;

}
  • 1
    Please provide a [mcve]. Btw, you have two `new` but only one `delete` smells like memory leak – 463035818_is_not_an_ai Feb 16 '17 at 18:23
  • 2
    "the function stops storing values" are you sure? Seems to me like the issue would arise when reading the string not storing it. You could just use a `std::vector` instead to hold the size instead of reading to nul and assuming that will terminate at the size of your string. – George Feb 16 '17 at 18:24
  • Make life simpler, use `std::vector`. – Cheers and hth. - Alf Feb 16 '17 at 18:27
  • The general idea seems sound. http://ideone.com/UssjvB. – R Sahu Feb 16 '17 at 18:27
  • I guess you could also wait until you're reading the string before bothering to convert, i.e. `aArray[i] = str[j]-'0';` -> `aArray[i] = str[j];` & then something like `for( int i = 0; m_number[i] != 0; ++i ) std::cout << m_number[i]-'0';` – George Feb 16 '17 at 18:31
  • @George Yes you're correct. The issue occurs while setting aArray to str. Whenever str == '0', the array stops storing values and won't store any subsequent values. I tried your suggestion which did allow me to store the string in the array, but once it tries to store it into m_number it then stops at the '0' value – Cayden Anderson Feb 16 '17 at 18:33
  • How are you creating this string str? – Florian p.i. Feb 16 '17 at 18:45
  • @CaydenAnderson -- This is a simple one-liner if you used `std::vector` for m_number, as [seen here](http://ideone.com/ku72VO). All of this dynamic allocation of temporary arrays is not necessary. – PaulMcKenzie Feb 16 '17 at 18:51
  • @Florianp.i. I'm just calling the constructor such as 'BigInteger("1238970")'. While loading this value in, the value of m_number should be 0798321 but would come out empty since the first value is a zero – Cayden Anderson Feb 16 '17 at 18:51
  • @CaydenAnderson -- It's even a one-liner if you don't use `std::vector` and used `new[]`. [See here](http://ideone.com/SlB7s0). – PaulMcKenzie Feb 16 '17 at 19:04
  • @Paul this seems like a much easier work around, but my complier is still stopping once the value is '0' in the string using this method. Could this possibly just be an error with Xcode? – Cayden Anderson Feb 16 '17 at 19:09
  • @CaydenAnderson [Cannot duplicate](http://ideone.com/VxkDtE) – PaulMcKenzie Feb 16 '17 at 19:20

1 Answers1

0

The problem is that uint8_t is treated like unsigned char.

So if you pass it through cout, it will interpret your 0 as index for ASCII, which is '\0' and stop. So you have to ensure cout doesn't interpret your integer information as character information.

See this for solutions and further information: uint8_t can't be printed with cout

Community
  • 1
  • 1
Florian p.i.
  • 622
  • 3
  • 7
  • The issue doesn't arise from std::cout as I never actually output the value, it's a problem in storing the value – Cayden Anderson Feb 21 '17 at 05:38
  • If you don`t use any output, how do you know that there is a problem? At which line of code can you detect its not storing a value? – Florian p.i. Feb 22 '17 at 22:09