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. Win32 Memory Allocation

Win32 Memory Allocation

Scheduled Pinned Locked Moved C / C++ / MFC
csstestingdebuggingbeta-testingregex
6 Posts 2 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.
  • A Offline
    A Offline
    Alan Chambers
    wrote on last edited by
    #1

    Hi all, I'm writing my own string library that needs to be super-fast (much faster than CString). I am well on the way but need an efficient way of allocating memory so that I don't get fragmentation when concatening strings etc. I have thought of a way for an allocation routine to do this whereby memory is always allocated as a base 2 block with the minimum being 8 bytes. This will allow the string to grow in size with little fragmentation on reallocation (and indeed less use of realloc). It will of course, always consume slightly more memory than is required to hold the string. All I need is a method for grabbing the highest order bit that describes the length of the string and bit shift it to the left e.g. say the string was "LENGTH" it is 6 chars long so the highest bit is (0100 - since 6 is 0110 in binary) and bit shift it to 1000. I would then malloc 8 bytes which is enough to hold the 6 char string and its 1 char null terminator. However, I'm stuck on finding the quickest method of getting the highest ranking bit. I know I can do it by testing 0100 & 1000, 0100 & 0100 etc. till I find a match, but I was just wondering if there is a better way? Any comments or suggestions on how to make a better string library would also be grateful. Alan. P.S. Also I notice that CString displays the actual string in the variable pane of the debug window, whereas mine displays {...} and you have to go to the next level to check what the string is, any ideas on how I can get it to display {"LENGTH"} would be brilliant too :) "When I left you I was but the learner, now I am the master" - Darth Vader

    J 2 Replies Last reply
    0
    • A Alan Chambers

      Hi all, I'm writing my own string library that needs to be super-fast (much faster than CString). I am well on the way but need an efficient way of allocating memory so that I don't get fragmentation when concatening strings etc. I have thought of a way for an allocation routine to do this whereby memory is always allocated as a base 2 block with the minimum being 8 bytes. This will allow the string to grow in size with little fragmentation on reallocation (and indeed less use of realloc). It will of course, always consume slightly more memory than is required to hold the string. All I need is a method for grabbing the highest order bit that describes the length of the string and bit shift it to the left e.g. say the string was "LENGTH" it is 6 chars long so the highest bit is (0100 - since 6 is 0110 in binary) and bit shift it to 1000. I would then malloc 8 bytes which is enough to hold the 6 char string and its 1 char null terminator. However, I'm stuck on finding the quickest method of getting the highest ranking bit. I know I can do it by testing 0100 & 1000, 0100 & 0100 etc. till I find a match, but I was just wondering if there is a better way? Any comments or suggestions on how to make a better string library would also be grateful. Alan. P.S. Also I notice that CString displays the actual string in the variable pane of the debug window, whereas mine displays {...} and you have to go to the next level to check what the string is, any ideas on how I can get it to display {"LENGTH"} would be brilliant too :) "When I left you I was but the learner, now I am the master" - Darth Vader

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      Follow this link[^] for a routine based on succesive approximations. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      A 1 Reply Last reply
      0
      • J Joaquin M Lopez Munoz

        Follow this link[^] for a routine based on succesive approximations. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        A Offline
        A Offline
        Alan Chambers
        wrote on last edited by
        #3

        Hello Joaquin, its been a while...:). Thanks for the URL, you missed out one directory shift by mistake though it should have been ftp://cap.connx.com/pub/bitcount/LOGS/LOG2.C[^] :)? Many Thanks, Alan. "When I left you I was but the learner, now I am the master" - Darth Vader

        J 1 Reply Last reply
        0
        • A Alan Chambers

          Hello Joaquin, its been a while...:). Thanks for the URL, you missed out one directory shift by mistake though it should have been ftp://cap.connx.com/pub/bitcount/LOGS/LOG2.C[^] :)? Many Thanks, Alan. "When I left you I was but the learner, now I am the master" - Darth Vader

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          Ummm. Both URLs point to different files. Yours is based on conversion to double, mine on binary search. Try again, I've just tested the URL and is working OK. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          1 Reply Last reply
          0
          • A Alan Chambers

            Hi all, I'm writing my own string library that needs to be super-fast (much faster than CString). I am well on the way but need an efficient way of allocating memory so that I don't get fragmentation when concatening strings etc. I have thought of a way for an allocation routine to do this whereby memory is always allocated as a base 2 block with the minimum being 8 bytes. This will allow the string to grow in size with little fragmentation on reallocation (and indeed less use of realloc). It will of course, always consume slightly more memory than is required to hold the string. All I need is a method for grabbing the highest order bit that describes the length of the string and bit shift it to the left e.g. say the string was "LENGTH" it is 6 chars long so the highest bit is (0100 - since 6 is 0110 in binary) and bit shift it to 1000. I would then malloc 8 bytes which is enough to hold the 6 char string and its 1 char null terminator. However, I'm stuck on finding the quickest method of getting the highest ranking bit. I know I can do it by testing 0100 & 1000, 0100 & 0100 etc. till I find a match, but I was just wondering if there is a better way? Any comments or suggestions on how to make a better string library would also be grateful. Alan. P.S. Also I notice that CString displays the actual string in the variable pane of the debug window, whereas mine displays {...} and you have to go to the next level to check what the string is, any ideas on how I can get it to display {"LENGTH"} would be brilliant too :) "When I left you I was but the learner, now I am the master" - Darth Vader

            J Offline
            J Offline
            Joaquin M Lopez Munoz
            wrote on last edited by
            #5

            P.S. Also I notice that CString displays the actual string in the variable pane of the debug window, whereas mine displays {...} and you have to go to the next level to check what the string is, any ideas on how I can get it to display {"LENGTH"} would be brilliant too This can be done by tweaking a file called autoexp.dat living somewhere inside the installation dir of Visual Studio. Check Oskar Wieland's article CStringW and CStringA [^] for an example. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            A 1 Reply Last reply
            0
            • J Joaquin M Lopez Munoz

              P.S. Also I notice that CString displays the actual string in the variable pane of the debug window, whereas mine displays {...} and you have to go to the next level to check what the string is, any ideas on how I can get it to display {"LENGTH"} would be brilliant too This can be done by tweaking a file called autoexp.dat living somewhere inside the installation dir of Visual Studio. Check Oskar Wieland's article CStringW and CStringA [^] for an example. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

              A Offline
              A Offline
              Alan Chambers
              wrote on last edited by
              #6

              Thanks very much Joaquin, this code will cut down my development time immensely! Thankyou. I am looking into the autoexp.dat file thing so hopefully I'll be able to see whats in the string a bit easier too. Many Thanks (again) Alan. "When I left you I was but the learner, now I am the master" - Darth Vader

              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