0

So I noticed something a bit strange. I set aside 4 GB of physical memory for my program. I had already allocated 2 GiB of storage and I tried to add 1 GiB using the code below. The program then throws a bad allocation saying that the memory is full. So i wonder if i allocate 1 GiB using.

char* p1 = new char[1073741824];

Does this resereve some additional storage that is not shown using task manager or resource monitor?

Using visual studio 2017 Windows 10.

Jimmy Joe
  • 41
  • 3
  • 3
    The reason that's happening is because you need a **contiguous** 1GB of memory. Likely, your leftover 2GB is fairly fragmented. – zzxyz Apr 12 '18 at 16:54
  • There are many duplicates to "why does my huge allocation fail?" [Here is one](https://stackoverflow.com/questions/35240572). – Drew Dormann Apr 12 '18 at 17:10
  • If this is an actual practical consideration, `VirtualAllocEx` can give you a little more fine-grained control of your allocations, by the way, and may succeed where `new` fails. – zzxyz Apr 12 '18 at 17:21
  • 1
    @zzxyz but then your code is no longer portable to other platforms - which is a shame. – Jesper Juhl Apr 12 '18 at 17:51
  • @JesperJuhl - I've worked on a couple apps that actually have a practical need for allocating huge amounts of memory (4-8GB), and at that point an OS abstraction layer is all but required (specific code for each platform). At 1GB, there's probably another solve. – zzxyz Apr 12 '18 at 18:54

1 Answers1

1

There is some overhead associated with an allocation, but that's fairly tiny - as @zzxyz said, your problem is probably memory fragmentation. There may be ~2GB memory available in total but there may not be 1GB of contiguous memory available (which is what you are explicitly requesting).

Any object you allocate storage for requires contiguous storage for its members. new[] requests memory for a contiguous array of objects, so the entire block of storage needs to be contiguous - that's a guarantee C++ gives you which can be quite useful. If you allocated each individual object on its own with new then only the storage for each object would need to be contiguous and all the objects could then be scattered all over memory in each their own little contiguous chunk. Note new[] versus new.

Jesper Juhl
  • 1
  • 3
  • 38
  • 63
  • oh is it unique for new to require contigous allocation or is it dynamic memory in general? – Jimmy Joe Apr 12 '18 at 17:43
  • @Jimmy Joe - Any object you allocate storage for requires contiguous storage for its members. `new[]` requests memory for a contiguous array of objects, so the entire block of storage needs to be contiguous - that's a guarantee C++ gives you which can be quite useful. If you allocated each *individual* object on its own with `new` then only the storage for each object would need to be contiguous and all the objects could then be scattered all over memory in each their own little contiguous chunk. Note `new[]` versus `new`. – Jesper Juhl Apr 12 '18 at 17:48
  • very intressting, thank you. I just finished reading a few paragraphs about dynamic memory allocation sadly they failed to mention this detail. – Jimmy Joe Apr 12 '18 at 18:06