allocation problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SSJBardock

    allocation problem

    please help me, I'm rewriting a program I made before because I lost the
    source and I want to allocate a number of text strings that are 500
    characters long.
    So I tried this:
    char *m_IDs[500]=(char *)malloc(m_Numb erOfStrings);

    That obviously doesn't work, but I can't remember how to do it.

    Thanks in advance,

    SSJBardock


  • Rolf Magnus

    #2
    Re: allocation problem

    SSJBardock wrote:
    [color=blue]
    > please help me, I'm rewriting a program I made before because I lost
    > the source and I want to allocate a number of text strings that are
    > 500 characters long.
    > So I tried this:
    > char *m_IDs[500]=(char *)malloc(m_Numb erOfStrings);
    >
    > That obviously doesn't work, but I can't remember how to do it.[/color]

    std::vector<std ::string> m_IDs(m_NumerOf Strings, std::string(500 ), ' ');

    This creates a vector and initializes it with m_NumberOfStrin gs strings,
    which are each initialized to 500 space characters.

    If you insist on making your life hard by using arrays and pointers to
    char:

    char** m_IDs = new char*[m_NumberOfStrin gs];
    for (int i = 0; i < m_NumberOfStrin gs; ++i)
    m_IDs[i] = new char[500];

    Note that those char arrays are uninitialized.

    Don't forget to delete them:

    for (int i = 0; i < m_NumberOfStrin gs; ++i)
    delete [] m_IDs[i];
    delete [] m_IDs;


    Comment

    • Rolf Magnus

      #3
      Re: allocation problem

      Rolf Magnus wrote:
      [color=blue]
      > std::vector<std ::string> m_IDs(m_NumerOf Strings, std::string(500 ),
      > ' ');[/color]

      Of course that must be:

      std::vector<std ::string> m_IDs(m_NumerOf Strings, std::string(500 , ' '));

      Comment

      Working...