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. Preferred size for memory block alloc?

Preferred size for memory block alloc?

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformance
7 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.
  • D Offline
    D Offline
    Dominik Reichl
    wrote on last edited by
    #1

    Hi! in my program I often have to allocate and dellocate buffers for strings. For this, I thought I could avoid memory fragmentation by using an own memory allocation routine which will allocate some more bytes than needed, so many as needed to fill a full block. What is the best size for such a "full" block? Thanks in advance! Dominik


    _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

    A 1 Reply Last reply
    0
    • D Dominik Reichl

      Hi! in my program I often have to allocate and dellocate buffers for strings. For this, I thought I could avoid memory fragmentation by using an own memory allocation routine which will allocate some more bytes than needed, so many as needed to fill a full block. What is the best size for such a "full" block? Thanks in advance! Dominik


      _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

      A Offline
      A Offline
      Anand Paranjpe
      wrote on last edited by
      #2

      If u r using string from STL, it gives facility of reserving memory space. Read more on Function reserve(). This will suffice the need. The chosen One :)

      D 1 Reply Last reply
      0
      • A Anand Paranjpe

        If u r using string from STL, it gives facility of reserving memory space. Read more on Function reserve(). This will suffice the need. The chosen One :)

        D Offline
        D Offline
        Dominik Reichl
        wrote on last edited by
        #3

        No, I am not using STL. I allocate the memory using 'new' and pointing with a char* to it. Dominik


        _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

        D 1 Reply Last reply
        0
        • D Dominik Reichl

          No, I am not using STL. I allocate the memory using 'new' and pointing with a char* to it. Dominik


          _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

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

          Would it be of any benefit to only allocate the memory once and just reuse it?


          Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

          D 1 Reply Last reply
          0
          • D David Crow

            Would it be of any benefit to only allocate the memory once and just reuse it?


            Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

            D Offline
            D Offline
            Dominik Reichl
            wrote on last edited by
            #5

            The maximum size of a string is variable (it could just be one character or 2000 for example). Also there isn't any string number limit (only one string or more than 1000). So I need to allocate them dynamically. Dominik


            _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

            D 1 Reply Last reply
            0
            • D Dominik Reichl

              The maximum size of a string is variable (it could just be one character or 2000 for example). Also there isn't any string number limit (only one string or more than 1000). So I need to allocate them dynamically. Dominik


              _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

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

              That's fine. I was just trying to ascertain whether you could minimize the impact on the memory manager. One idea, which is how MFC's CString class works, is to only reallocate when necessary. Something like:

              char *pBuffer;
              int amount_currently_allocated = 0;

              while (...)
              {
              amount_needed = ???;

              if (amount\_needed > amount\_currently\_allocated)
              {
                  delete \[\] pBuffer;
                  pBuffer = new char\[amount\_needed\];
                  amount\_currently\_allocated = amount\_needed;
              }
              
              ...
              

              }


              Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

              D 1 Reply Last reply
              0
              • D David Crow

                That's fine. I was just trying to ascertain whether you could minimize the impact on the memory manager. One idea, which is how MFC's CString class works, is to only reallocate when necessary. Something like:

                char *pBuffer;
                int amount_currently_allocated = 0;

                while (...)
                {
                amount_needed = ???;

                if (amount\_needed > amount\_currently\_allocated)
                {
                    delete \[\] pBuffer;
                    pBuffer = new char\[amount\_needed\];
                    amount\_currently\_allocated = amount\_needed;
                }
                
                ...
                

                }


                Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

                D Offline
                D Offline
                Dominik Reichl
                wrote on last edited by
                #7

                DavidCrow wrote: Five birds are sitting on a fence. Three of them decide to fly off. How many are left? 2 ??? Dominik


                _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

                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