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. Memory buffer size

Memory buffer size

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
10 Posts 4 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.
  • H Offline
    H Offline
    hatemtalbi
    wrote on last edited by
    #1

    Hi, Is there any way to get the size of an allocated memory buffer knowing only the its address? Thank you all :)

    V J 2 Replies Last reply
    0
    • H hatemtalbi

      Hi, Is there any way to get the size of an allocated memory buffer knowing only the its address? Thank you all :)

      V Offline
      V Offline
      Viorel
      wrote on last edited by
      #2

      Maybe _msize can help?

      H 1 Reply Last reply
      0
      • V Viorel

        Maybe _msize can help?

        H Offline
        H Offline
        hatemtalbi
        wrote on last edited by
        #3

        No :( it doesn't worked for me !!! The _msize function returns the size, in bytes, of the memory block allocated by a call to calloc, malloc, or realloc. Thank you anyway

        V 1 Reply Last reply
        0
        • H hatemtalbi

          Hi, Is there any way to get the size of an allocated memory buffer knowing only the its address? Thank you all :)

          J Offline
          J Offline
          James R Twine
          wrote on last edited by
          #4

          The short answer, is no.  The long answer is that if you know something about the memory, you may be able to.  For example, if it is a BSTR you can look 4 bytes before its "handled" address or use SysStringLen(...).    If you know that the memory came from a particular heap (or heap manager), you may be able to crack the heap structures used to maintain memory allocations.  You also need to know where the memory came from in order to use functions like _msize(...), _msize_dbg(...), GlobalSize(...), etc.    Peace! -=- James


          If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
          Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
          DeleteFXPFiles & CheckFavorites (Please rate this post!)

          H 1 Reply Last reply
          0
          • J James R Twine

            The short answer, is no.  The long answer is that if you know something about the memory, you may be able to.  For example, if it is a BSTR you can look 4 bytes before its "handled" address or use SysStringLen(...).    If you know that the memory came from a particular heap (or heap manager), you may be able to crack the heap structures used to maintain memory allocations.  You also need to know where the memory came from in order to use functions like _msize(...), _msize_dbg(...), GlobalSize(...), etc.    Peace! -=- James


            If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
            Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
            DeleteFXPFiles & CheckFavorites (Please rate this post!)

            H Offline
            H Offline
            hatemtalbi
            wrote on last edited by
            #5

            Here it is: LPVOID data = MapViewOfFile(m_hMap, _dwDesiredAccess, 0, _dwOffset, _dwNbBytes); I need to know the size of the data buffer. I call this code after resizing a map file with 8ko but I can acess only the first 4ko :( could someone help me?!!

            D 1 Reply Last reply
            0
            • H hatemtalbi

              Here it is: LPVOID data = MapViewOfFile(m_hMap, _dwDesiredAccess, 0, _dwOffset, _dwNbBytes); I need to know the size of the data buffer. I call this code after resizing a map file with 8ko but I can acess only the first 4ko :( could someone help me?!!

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

              hatemtalbi wrote:

              I need to know the size of the data buffer.

              Isn't that what _dwNbBytes represents?

              hatemtalbi wrote:

              I call this code after resizing a map file with 8ko but I can acess only the first 4ko

              What in the world is "ko?"


              "The largest fire starts but with the smallest spark." - David Crow

              "Judge not by the eye but by the heart." - Native American Proverb

              H 1 Reply Last reply
              0
              • H hatemtalbi

                No :( it doesn't worked for me !!! The _msize function returns the size, in bytes, of the memory block allocated by a call to calloc, malloc, or realloc. Thank you anyway

                V Offline
                V Offline
                Viorel
                wrote on last edited by
                #7

                In case of objects allocated with new operator, the number of items seems to be stored in a word before the pointer:

                MyClass * a = new MyClass[10];
                __int32 size = *(((__int32*)a) - 1); // contains 10
                

                This works only when the class contains destructor. The size does not seem to be stored in such manner for destructor-less objects and for fundamental types. But for this cases, _msize appears to return the size in bytes.

                1 Reply Last reply
                0
                • D David Crow

                  hatemtalbi wrote:

                  I need to know the size of the data buffer.

                  Isn't that what _dwNbBytes represents?

                  hatemtalbi wrote:

                  I call this code after resizing a map file with 8ko but I can acess only the first 4ko

                  What in the world is "ko?"


                  "The largest fire starts but with the smallest spark." - David Crow

                  "Judge not by the eye but by the heart." - Native American Proverb

                  H Offline
                  H Offline
                  hatemtalbi
                  wrote on last edited by
                  #8

                  no, _dwNbBytes == 0 ko mean kb == 1024 bytes thx

                  D 1 Reply Last reply
                  0
                  • H hatemtalbi

                    no, _dwNbBytes == 0 ko mean kb == 1024 bytes thx

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

                    hatemtalbi wrote:

                    no, _dwNbBytes == 0

                    Then you simply need to look at the size of the file, since the entire file was mapped.


                    "The largest fire starts but with the smallest spark." - David Crow

                    "Judge not by the eye but by the heart." - Native American Proverb

                    H 1 Reply Last reply
                    0
                    • D David Crow

                      hatemtalbi wrote:

                      no, _dwNbBytes == 0

                      Then you simply need to look at the size of the file, since the entire file was mapped.


                      "The largest fire starts but with the smallest spark." - David Crow

                      "Judge not by the eye but by the heart." - Native American Proverb

                      H Offline
                      H Offline
                      hatemtalbi
                      wrote on last edited by
                      #10

                      that s the problem!!! it seems that the size of the file is larger than the memory buffer returned. I don't understand why :(

                      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