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. Best way to allocate large char arrays with malloc?

Best way to allocate large char arrays with malloc?

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
7 Posts 5 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.
  • T Offline
    T Offline
    Tommy Svensson
    wrote on last edited by
    #1

    Hi all, I wish to malloc a large char array (to handle appr. 1 MB file sizes), how can I do that without cracking my computer wide open? /Tommy

    J T C 3 Replies Last reply
    0
    • T Tommy Svensson

      Hi all, I wish to malloc a large char array (to handle appr. 1 MB file sizes), how can I do that without cracking my computer wide open? /Tommy

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

      If you absolutely need to have a contiguous block of memory of 1MB, go ahead and request to malloc a block that big. Chances are there'll be no serious problems, as 1MB is handlable by PCs these days, and moreover the virtual paging mechanism will help liven things up. Apart from this, maybe you can get by with some block of memory that grows on demand, so that only gets to these huge sizes when actually required. If so, have a look at std::vector, which automates this behavior for you. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      A 1 Reply Last reply
      0
      • T Tommy Svensson

        Hi all, I wish to malloc a large char array (to handle appr. 1 MB file sizes), how can I do that without cracking my computer wide open? /Tommy

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

        Malloc will work just fine with one warning. The only problem I have ever run into with allocating large blocks of memory is that if you allocate/free that block a lot, you run the risk of fragmenting your virtual address space. This can happen if you free that 1MB block and then part of it is used by a smaller allocation. Then when you go and try to allocate 1MB again, the system has to allocate a new 1MB block. 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?

        1 Reply Last reply
        0
        • J Joaquin M Lopez Munoz

          If you absolutely need to have a contiguous block of memory of 1MB, go ahead and request to malloc a block that big. Chances are there'll be no serious problems, as 1MB is handlable by PCs these days, and moreover the virtual paging mechanism will help liven things up. Apart from this, maybe you can get by with some block of memory that grows on demand, so that only gets to these huge sizes when actually required. If so, have a look at std::vector, which automates this behavior for you. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          A Offline
          A Offline
          Albert Pascual
          wrote on last edited by
          #4

          Joaquin is right! Better allocate the memory as you need it: with malloc and realloc I normally use! So you don't have to allocate on 1MB up front! Al

          1 Reply Last reply
          0
          • T Tommy Svensson

            Hi all, I wish to malloc a large char array (to handle appr. 1 MB file sizes), how can I do that without cracking my computer wide open? /Tommy

            C Offline
            C Offline
            Chris Hafey
            wrote on last edited by
            #5

            Have you considered memory mapping the file? Not only is this faster than reading the file into a buffer, but it will be read into memory on demand (as you access the different parts of the file). Chris

            T 1 Reply Last reply
            0
            • C Chris Hafey

              Have you considered memory mapping the file? Not only is this faster than reading the file into a buffer, but it will be read into memory on demand (as you access the different parts of the file). Chris

              T Offline
              T Offline
              Tommy Svensson
              wrote on last edited by
              #6

              Nope, haven't... How do I do that? /T

              C 1 Reply Last reply
              0
              • T Tommy Svensson

                Nope, haven't... How do I do that? /T

                C Offline
                C Offline
                Chris Hafey
                wrote on last edited by
                #7

                CodeProject may have some examples, I am not sure. Look through the help for the function MapViewOfFile(). Richter also talks about memory mapping in depth in his book advanced windows programming. Chris

                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