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. Dynamic memory allocation of 2-D array

Dynamic memory allocation of 2-D array

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsdata-structuresperformancetutorial
6 Posts 5 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.
  • S Offline
    S Offline
    Sarvan AL
    wrote on last edited by
    #1

    Hi, I'd like to write a function, which returns an array of strings. But the size of the array is unknown. For example, it has to create a string array with the nos. I enter (say, "1", "2", "4", "9".. , and return back to the caller function when I enter 0. It has to grow dynamically. But I don't want to do it with built-in classes like list, vector and so on. I'd like to use 'new' operator or 'malloc/realloc' functions. Pls suggest me a good solution. Thanks in advance, Sarvan AL

    O R K 3 Replies Last reply
    0
    • S Sarvan AL

      Hi, I'd like to write a function, which returns an array of strings. But the size of the array is unknown. For example, it has to create a string array with the nos. I enter (say, "1", "2", "4", "9".. , and return back to the caller function when I enter 0. It has to grow dynamically. But I don't want to do it with built-in classes like list, vector and so on. I'd like to use 'new' operator or 'malloc/realloc' functions. Pls suggest me a good solution. Thanks in advance, Sarvan AL

      O Offline
      O Offline
      Owner drawn
      wrote on last edited by
      #2
      int twoDArray = new int[rows*cols];
      

      Jesus Lives Forever - Amen:rose:

      --Owner drawn:rose: --An eye for an eye makes the whole world blind. --If you find my post helpful then do rate it. --Jesus is Lord:rose:

      1 Reply Last reply
      0
      • S Sarvan AL

        Hi, I'd like to write a function, which returns an array of strings. But the size of the array is unknown. For example, it has to create a string array with the nos. I enter (say, "1", "2", "4", "9".. , and return back to the caller function when I enter 0. It has to grow dynamically. But I don't want to do it with built-in classes like list, vector and so on. I'd like to use 'new' operator or 'malloc/realloc' functions. Pls suggest me a good solution. Thanks in advance, Sarvan AL

        R Offline
        R Offline
        Rage
        wrote on last edited by
        #3

        Ever considered using STLs ? ~RaGE();

        1 Reply Last reply
        0
        • S Sarvan AL

          Hi, I'd like to write a function, which returns an array of strings. But the size of the array is unknown. For example, it has to create a string array with the nos. I enter (say, "1", "2", "4", "9".. , and return back to the caller function when I enter 0. It has to grow dynamically. But I don't want to do it with built-in classes like list, vector and so on. I'd like to use 'new' operator or 'malloc/realloc' functions. Pls suggest me a good solution. Thanks in advance, Sarvan AL

          K Offline
          K Offline
          kakan
          wrote on last edited by
          #4

          Google for the head line of your question: Dynamic memory allocation of 2-D array I got 199000 hits, several of them uses malloc.

          R 1 Reply Last reply
          0
          • K kakan

            Google for the head line of your question: Dynamic memory allocation of 2-D array I got 199000 hits, several of them uses malloc.

            R Offline
            R Offline
            RadioOpa
            wrote on last edited by
            #5

            "malloc" should nowadays be replaced with "new" as my Cpp teacher told me recently

            K 1 Reply Last reply
            0
            • R RadioOpa

              "malloc" should nowadays be replaced with "new" as my Cpp teacher told me recently

              K Offline
              K Offline
              kakan
              wrote on last edited by
              #6

              I quote the original poster: "I'd like to use 'new' operator or 'malloc/realloc' functions. Pls suggest me a good solution." The original poster is totally free to use any technique available, as far as I'm concerned. I'm not responsible neither for the hits gotten from Google nor what sample the original poster uses. But your teacher has got a point. In C++, it's preferred. I plain C, however, new() isn't available. And that's where malloc/realloc comes in, amongst others.

              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