Thread: allocation problem

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    18

    allocation problem

    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_NumberOfStrings);

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

    Thanks in advance,

    SSJBardock

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Since your using C++ it is prefered to use new.
    Code:
    // allocate an array of pointers to char*
    char** m_IDs = new char*[m_NumberOfStrings];
    // allocate memory for each one
    ...
    m_ID[i] = new char[nLengthOfString];
    ...
    delete [] m_ID[i];
    ...
    delete [] m_ID;
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    18
    Thanks, that almost works, but for some reason when I make m_NumberOfStrings higher than 1015 (nLengthOfString being 500) I get an assert error.

    Do you have any idea what's the problem and how to fix it?

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I can't say for sure, but in any case you're trying to allocate half a megabyte in that statement

    But since this is C++ and you're using an array of c-strings, I suggest you try using a std::vector of std::string's. Just a suggestion Generally in C++, vectors are preferred over arrays since they can do pretty much everything arrays can but easier and in many cases better, and strings are preferred over char* for the same reason.

    Code:
    #include <vector>
    
    std::vector<std::string> m_IDs;
    m_IDs.push_back("SomeID");
    m_IDs.push_back("SomeOtherID");
    
    for(int i = 0; i < m_IDs.size(); ++i)
    {
         doSomething(m_IDs[i]);
    }
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  2. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  3. memory allocation problem....help..
    By CyC|OpS in forum C Programming
    Replies: 8
    Last Post: 10-18-2002, 09:26 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Pointer and memory allocation problem
    By Dual-Catfish in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2002, 11:13 AM