0

My problem is that I don't know how to convert int value to char array char* m_value. I tried to use itoa but it doesn't work. itoa(m_val, m_wartosc, 10); Maybe there is some other function to do this ?

Main.cpp

int main(int argc, char *argv[])
{
    LargeNumber l1;
    LargeNumber l3(172839); //how to convert this int to char*

    return 0;
}

LargeNumber.h

    class LargeNumber{

            public:

                LargeNumber()
                { 
                    m_array = "0"; //zero for no arg.
                }
                LargeNumber(int val):m_val(val)
                {
                    itoa(m_val, m_array, 10);  //doesn't work
                    //sprintf(m_array, "%d", m_val);
                }



                LargeNumber(const LargeNumber& p):m_array(p.m_array)
                { }  //copy constructor


                ~LargeNumber(){
                    delete []m_array;     //for object with new    
                }
               public: //should be private
                int m_val;
                char* m_array;

};
mathewM
  • 139
  • 1
  • 1
  • 14
  • Why do you need to, you already have a constructor that takes an `int`. – Luchian Grigore May 28 '12 at 14:30
  • Why do you want to store your number in base-10 as an ASCII string? – Oliver Charlesworth May 28 '12 at 14:32
  • I need to have both of them for my program. I have already one which will take `"string"`. But there is a need for one which will take int value. – mathewM May 28 '12 at 14:33
  • I know that storeing number as a char* is wrong, but I need to manage with that. – mathewM May 28 '12 at 14:35
  • 1
    Side note, if default constructor is used the `delete[] m_array` in the destructor will be attempting to deallocate read-only memory. – hmjd May 28 '12 at 14:36
  • Is there a reason `char*` is being used instead of `std::string` for `m_array`? – hmjd May 28 '12 at 14:39
  • You are following a Detonator pattern from resign patterns (http://www.lsd.ic.unicamp.br/~oliva/fun/prog/resign-patterns) with: 1. deleting memory of a literal after creating with default constructor. 2. Not allocating memory for an array you are trying to use (m_array). 3. Sharing pointer in your copy constructor - it will be then double-deleted. Conclusion - use `std::string` instead of `char *` – Tadeusz Kopec for Ukraine May 28 '12 at 14:41

4 Answers4

5

The simple answer is: don't. For two reasons:

  • As you can see from all the (wrong) other answers, memory management is tricky and bug-prone.
  • I can't see how storing your value in base-10, in an ASCII string, could possibly be useful. (Compared to, say, a base-232 representation.)

But if you really must store it this way, you will need to allocate the relevant amount of memory, use snprintf to convert (itoa is a non-standard function), and remember to free the memory at the correct time(s) (you will have to read and understand about the Rule of Three).

I would strongly recommend using a std::string instead of a raw C-style array, because it will at least deal with its own memory management, and you will then be able to populate it with a std::stringstream.

Community
  • 1
  • 1
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
0

The second argument of itoa() needs to be an array in memory large enough to store the null-terminated string. An example:

int number = 172839;
char buffer[10];
itoa(number,buffer,10);
Adam27X
  • 889
  • 1
  • 7
  • 16
0
LargeNumber(int val):m_val(val)
        {
            std::stringstream stream;
            stream << val;
            m_array = new char[stream.str().size()];
            strcpy(m_array, stream.str().c_str());
        }
sithereal
  • 1,656
  • 9
  • 16
0

You have to first allocate the array with

m_array = new char[20]

in constructor before calling iota. the iota doesnt allocate memory.