Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Declare string array in heap

Declare string array in heap

Scheduled Pinned Locked Moved C / C++ / MFC
csharpdata-structurestutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aqueel
    wrote on last edited by
    #1

    Hi Can anyone plz tell me how to declare a string array in heap? I also dont want to specify its size at the time of declaration. Thanks

    We Believe in Excellence www.aqueelmirza.cjb.net

    J CPalliniC 2 Replies Last reply
    0
    • A Aqueel

      Hi Can anyone plz tell me how to declare a string array in heap? I also dont want to specify its size at the time of declaration. Thanks

      We Believe in Excellence www.aqueelmirza.cjb.net

      J Offline
      J Offline
      John R Shaw
      wrote on last edited by
      #2

      std::vectorstd::string string_array; At least that is how it is done in C++. Of course that is not really a heap only version, because for that you would have to know the size of the array and the maximum length of any string placed in the array. But for your question it is close enough to qualify as a resonable answer. INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

      1 Reply Last reply
      0
      • A Aqueel

        Hi Can anyone plz tell me how to declare a string array in heap? I also dont want to specify its size at the time of declaration. Thanks

        We Believe in Excellence www.aqueelmirza.cjb.net

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        Of course it depends on what you choose as a suitable string. For instance C-like strings are simply (NULL-terminated) character arrays, hence you can do something like:

        char ** myStringArray;
        myStringArray = new char * [3];
        myStringArray[0] = "hello world";
        myStringArray[1] = new char[10];
        memset(myStringArray[1], '*',9);
        myStringArray[1][9]='\0';
        myStringArray[2] = strdup("Hi");
        //Clean-up
        delete [] myStringArray[1];
        free (myStringArray[2]);
        delete [] myStringArray;

        On the other hand, MFC CString or std::string are objects controlling themselves the inner character buffer, therefore you can do, for instance:

        CString * myStringArray;
        myStringArray = new CString[2];
        myStringArray[0]="Hello World";
        myStringArray[0] += "!";// note CString dynamically controls its buffer
        myStringArray[1]=CString('*',9);

        // Clean-Up
        delete [] myStringArray;

        Hope that helps :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups