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. (Console C++)Resizing mallocation?

(Console C++)Resizing mallocation?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
8 Posts 6 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.
  • M Offline
    M Offline
    Mariano Lopez Gappa
    wrote on last edited by
    #1

    Hi. This is not a Visual C++ question. Also, it'd be great if it worked under ANSI C. In this function I'm writing, strings start to come up and need to be saved on a big char* one next to the other, say: "yellow", "one", "banana" -> "yellowonebanana" thing is that final length is unknown, it could be 1000b as well as it could be 0b. so I looked it up from Google and only solution found was to do the algorythm twice, where in the first one I calculate length, then malloc, then do again and start storing. Is there any way to allocate and resize allocation dynamically? Thx in advance. Mariano Lopez-Gappa.

    R T D L D 5 Replies Last reply
    0
    • M Mariano Lopez Gappa

      Hi. This is not a Visual C++ question. Also, it'd be great if it worked under ANSI C. In this function I'm writing, strings start to come up and need to be saved on a big char* one next to the other, say: "yellow", "one", "banana" -> "yellowonebanana" thing is that final length is unknown, it could be 1000b as well as it could be 0b. so I looked it up from Google and only solution found was to do the algorythm twice, where in the first one I calculate length, then malloc, then do again and start storing. Is there any way to allocate and resize allocation dynamically? Thx in advance. Mariano Lopez-Gappa.

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      realloc(), but it's expensive. :( /ravi My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

      1 Reply Last reply
      0
      • M Mariano Lopez Gappa

        Hi. This is not a Visual C++ question. Also, it'd be great if it worked under ANSI C. In this function I'm writing, strings start to come up and need to be saved on a big char* one next to the other, say: "yellow", "one", "banana" -> "yellowonebanana" thing is that final length is unknown, it could be 1000b as well as it could be 0b. so I looked it up from Google and only solution found was to do the algorythm twice, where in the first one I calculate length, then malloc, then do again and start storing. Is there any way to allocate and resize allocation dynamically? Thx in advance. Mariano Lopez-Gappa.

        T Offline
        T Offline
        Tim Smith
        wrote on last edited by
        #3

        realloc works just fine. However, it is best if you use some type of doubling algorithm on the buffer size. Thus you reduce the number of reallocations. Tim Smith I'm going to patent thought. I have yet to see any prior art.

        1 Reply Last reply
        0
        • M Mariano Lopez Gappa

          Hi. This is not a Visual C++ question. Also, it'd be great if it worked under ANSI C. In this function I'm writing, strings start to come up and need to be saved on a big char* one next to the other, say: "yellow", "one", "banana" -> "yellowonebanana" thing is that final length is unknown, it could be 1000b as well as it could be 0b. so I looked it up from Google and only solution found was to do the algorythm twice, where in the first one I calculate length, then malloc, then do again and start storing. Is there any way to allocate and resize allocation dynamically? Thx in advance. Mariano Lopez-Gappa.

          D Offline
          D Offline
          douglasjordan
          wrote on last edited by
          #4

          You could create a file on the local drive and store as much as the drive would hold. There would be no need for any memory allocation or re-allocation. If you don't need speed, its an option.

          M 1 Reply Last reply
          0
          • D douglasjordan

            You could create a file on the local drive and store as much as the drive would hold. There would be no need for any memory allocation or re-allocation. If you don't need speed, its an option.

            M Offline
            M Offline
            Mariano Lopez Gappa
            wrote on last edited by
            #5

            Many thanks to all 3 answers. I'm gonna go for the realloc as it looks like it fits best my needs. Speed is not really an issue but I can't use disk. Thanks again. Case closed. :D

            1 Reply Last reply
            0
            • M Mariano Lopez Gappa

              Hi. This is not a Visual C++ question. Also, it'd be great if it worked under ANSI C. In this function I'm writing, strings start to come up and need to be saved on a big char* one next to the other, say: "yellow", "one", "banana" -> "yellowonebanana" thing is that final length is unknown, it could be 1000b as well as it could be 0b. so I looked it up from Google and only solution found was to do the algorythm twice, where in the first one I calculate length, then malloc, then do again and start storing. Is there any way to allocate and resize allocation dynamically? Thx in advance. Mariano Lopez-Gappa.

              L Offline
              L Offline
              LighthouseJ
              wrote on last edited by
              #6

              Can't you do this: 1. Read in the string to a temporary max-length memory location. 2. Allocate the strings' memory using the strlen() function. 3. Use strcpy() to move the string into it's position 4. Repeat steps 1-3 until you are done 5. Deallocate the temporary string. That's the easiest way I can I see doing it straight-forward and using only ANSI-C commands.

              1 Reply Last reply
              0
              • M Mariano Lopez Gappa

                Hi. This is not a Visual C++ question. Also, it'd be great if it worked under ANSI C. In this function I'm writing, strings start to come up and need to be saved on a big char* one next to the other, say: "yellow", "one", "banana" -> "yellowonebanana" thing is that final length is unknown, it could be 1000b as well as it could be 0b. so I looked it up from Google and only solution found was to do the algorythm twice, where in the first one I calculate length, then malloc, then do again and start storing. Is there any way to allocate and resize allocation dynamically? Thx in advance. Mariano Lopez-Gappa.

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                Mariano Lopez-Gappa wrote:

                thing is that final length is unknown, it could be 1000b as well as it could be 0b.

                Is that the only difference? Memory is cheap these days. Unless you are talking about several MB of allocations, just allocate 1KB for each instance and be done with it. Yes, there might be some waste here and there, but that's a whole lot more efficient than reallocating memory.


                "Take only what you need and leave the land as you found it." - Native American Proverb

                T 1 Reply Last reply
                0
                • D David Crow

                  Mariano Lopez-Gappa wrote:

                  thing is that final length is unknown, it could be 1000b as well as it could be 0b.

                  Is that the only difference? Memory is cheap these days. Unless you are talking about several MB of allocations, just allocate 1KB for each instance and be done with it. Yes, there might be some waste here and there, but that's a whole lot more efficient than reallocating memory.


                  "Take only what you need and leave the land as you found it." - Native American Proverb

                  T Offline
                  T Offline
                  Tim Smith
                  wrote on last edited by
                  #8

                  LOL. I misread. If he is only talking about 1000b > a > 0b, then I agree with you. Start with a large 1000b buffer. If you run out of room, double the size and try again. When done, return a new allocated buffer of the final size (not a required step if memory isn't an issue). Tim Smith I'm going to patent thought. I have yet to see any prior art.

                  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