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. Questions regarding memory management (MALLOC)

Questions regarding memory management (MALLOC)

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structuresdebuggingperformanceannouncement
5 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.
  • J Offline
    J Offline
    Jesse Rosalia
    wrote on last edited by
    #1

    First question is...when using malloc to allocate memory and _msize to retrieve the size in a Release build, the value _msize returns, and also the value stored in the memory block header is rounded up to the nearest multiple of 8 (i.e. 7 is rounded to 8, 100 is rounded to 104, etc). This is not the case in a Debug build. Is there any way to tell the heap manager not to round up? It wouldnt even be so bad if it allocated the rounded up number of bytes, but as it is, if I tell malloc to allocate 100 bytes, it allocates 100 bytes and then tells me it allocated 104, and that isnt good for so many reasons. ;) Next question is...when you use new to allocate an array of structures, such as... CMyClass *pVar = new CMyClass[count] I have one program which will add 4 bytes to the size passed to the new operator, and store count in those 4 bytes, and I have another program in which it does not do that. Is there a way to turn this functionality on or off? It must be some compile time setting, because in the program where it does that, there is an 'add 4' assembler instruction right before the call to new. Thanks a bunch in advance to whomever can answer these 2 :) -Jesse

    P 1 Reply Last reply
    0
    • J Jesse Rosalia

      First question is...when using malloc to allocate memory and _msize to retrieve the size in a Release build, the value _msize returns, and also the value stored in the memory block header is rounded up to the nearest multiple of 8 (i.e. 7 is rounded to 8, 100 is rounded to 104, etc). This is not the case in a Debug build. Is there any way to tell the heap manager not to round up? It wouldnt even be so bad if it allocated the rounded up number of bytes, but as it is, if I tell malloc to allocate 100 bytes, it allocates 100 bytes and then tells me it allocated 104, and that isnt good for so many reasons. ;) Next question is...when you use new to allocate an array of structures, such as... CMyClass *pVar = new CMyClass[count] I have one program which will add 4 bytes to the size passed to the new operator, and store count in those 4 bytes, and I have another program in which it does not do that. Is there a way to turn this functionality on or off? It must be some compile time setting, because in the program where it does that, there is an 'add 4' assembler instruction right before the call to new. Thanks a bunch in advance to whomever can answer these 2 :) -Jesse

      P Offline
      P Offline
      Paul M Watt
      wrote on last edited by
      #2

      Jesse Rosalia wrote: First question is...when using malloc to allocate memory and _msize to retrieve the size in a Release build, the value _msize returns, and also the value stored in the memory block header is rounded up to the nearest multiple of 8 malloc attempts to allocate a buffer that is at least the size that you asked for, it may allocate a block that is larger that the requested size. So there is really nothing that you can do about the size rounding. In your question about new, is the object that you are allocating memory for the same object in both programs?

      J 1 Reply Last reply
      0
      • P Paul M Watt

        Jesse Rosalia wrote: First question is...when using malloc to allocate memory and _msize to retrieve the size in a Release build, the value _msize returns, and also the value stored in the memory block header is rounded up to the nearest multiple of 8 malloc attempts to allocate a buffer that is at least the size that you asked for, it may allocate a block that is larger that the requested size. So there is really nothing that you can do about the size rounding. In your question about new, is the object that you are allocating memory for the same object in both programs?

        J Offline
        J Offline
        Jesse Rosalia
        wrote on last edited by
        #3

        No, I was using different objects in the 2 different programs...but I think Ive all but answered all the questions I had with that through good ol fashion research and dumb luck :). As for the malloc issue...it doesnt, in my experience, actually allocate the rounded up size in bytes...it allocates what you tell it to, and then reports that it allocated the rounded up size. Am I missing something, or do I need to have something defined somewhere to make this work properly? Thanks for your response, by the way :) -Jesse Rosalia

        T 1 Reply Last reply
        0
        • J Jesse Rosalia

          No, I was using different objects in the 2 different programs...but I think Ive all but answered all the questions I had with that through good ol fashion research and dumb luck :). As for the malloc issue...it doesnt, in my experience, actually allocate the rounded up size in bytes...it allocates what you tell it to, and then reports that it allocated the rounded up size. Am I missing something, or do I need to have something defined somewhere to make this work properly? Thanks for your response, by the way :) -Jesse Rosalia

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

          It is actually the case that malloc can allocate a larger block. This is a common technique used to increase memory allocation speed. You have a quick allocation lists for specific memory ranges. Such as 0-32 bytes uses one queue, 33-64 uses another queue, etc... Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

          J 1 Reply Last reply
          0
          • T Tim Smith

            It is actually the case that malloc can allocate a larger block. This is a common technique used to increase memory allocation speed. You have a quick allocation lists for specific memory ranges. Such as 0-32 bytes uses one queue, 33-64 uses another queue, etc... Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

            J Offline
            J Offline
            Jesse Rosalia
            wrote on last edited by
            #5

            Ahhh hah!...Thanks for your reply...as I was formulating a response to this it dawned on me that I never tried to allocate the memory to its prefered size, and check the headers and footers then...in fact you are correct that it does allocate the reported size. This also brought an interesting quirk with calloc to my attention...it will write 0s to the requested size, not the allocated size...very confusing. Thanks for your help...I think I have a good grip on this stuff now. :) -Jesse Rosalia

            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