Best way to allocate large char arrays with malloc?
-
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
-
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
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 atstd::vector
, which automates this behavior for you. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
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
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?
-
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 atstd::vector
, which automates this behavior for you. Joaquín M López Muñoz Telefónica, Investigación y DesarrolloJoaquin 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
-
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
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
-
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
Nope, haven't... How do I do that? /T
-
Nope, haven't... How do I do that? /T
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