-2

I have the following code:

#include <iostream>

int main()
{
    try {
        while (true)
            int* ptr = new int[10000000L];
    } catch (const std::bad_alloc& e) {
        std::cout << e.what();
        return EXIT_FAILURE;

    }
    return EXIT_SUCCESS;
}

The main problem is that the std::bad_alloc exception is never caught, the program continues to run endlessly without any errors. Of course, all of my 8GB RAM is filled to 95%, then it starts using the utilizing swap memory. As a result, the error about the lack of memory does not pop up.

I'm using a common Windows 10 OS.

How to fix that program to disable using utilizing swap memory?

1 Answers1

4

First, modern computers use virtual memory. In a virtual memory system, the pointer value of an address in memory space has an offset into a page and a page index in it. (Typically pages are 4k in size, so 12 bits of page offset).

The physical location of the page in physical memory may not exist, or it might exist at a completely different offset. There is a table mapping the page number to where the data actually is. This table can include a physical page offset, or a trap that causes code to be executed in the OS if an attempt is made to access that page.

So while you have only 8 GB of physical memory, this does not limit the amount of memory a process can address.

This is also how different processes can't touch each other's memory; they exist in different address spaces. The same pointer value in one process does not refer to the same page, because they have distinct page tables. (In windows, the processes share a kernel address space; but outside of kernel mode you can't access those pages anyhow, nor do safe OS APIs let you inject user-mode values into any kernel-mode pointer)

Second, you are only asking for address space to be reserved. A 64 bit computer can have up to 2^64 memory addresses (each 8 bits), or over 10^19. You are allocating 10^7 at a time. This won't run out of 64 bit address space for 10^12 iterations (1000 billion). If you can do 10^7 allocations per second, this should take about a day.

Now, not all 64 bit systems have the full 64 bit memory space. So it could happen earlier.

Many OS's permit you to request memory space, and if never touched don't actually assign you any physical memory or even any space on disk.

Third, in C++ if you allocate memory and lose track of all pointers to it, this is equivalent to never allocating the memory. The C++ standard permits compilers to detect noop allocations and not do them.

In short, if you want to know how much memory you have, find an OS API that tells you.

If you want to actually reserve physical memory, that generally isn't possible from user-mode code. You'll have to have special privileges, like be a driver or be part of the windows OS itself. And drivers have to be signed by the manufacturer of the hardware.

There are ways to request that your memory not be swapped to disk, but these are only going to be requests. Look up Virtual Memory Functions in the windows API. VirtualLock, for example. Using this is a really bad idea in 999/1000 cases.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524