What happens when the number of possible virtual addresses is exceeded

Suppose I am writing a program for an environment that has 32 bits for virtual space addresses (addresses 4294967296), what happens if I create more than 4294967296 variables that effectively exceed the number of possible addresses? Or does all the programs in the environment share more than 4294967296 addresses?

+3


source to share


5 answers


what happens if more than 4294967296 variables are created

I assume that you are very confused. You are not creating as many variables. You practically use C dynamic memory allocation (using malloc as well as free and friends ) or C ++ dynamic memory allocation (like heap management , above with low memory levels , like ::operator new

which many implementations use malloc

).

Note that malloc

(in C) and new

(in C ++) do not create fresh variables. They allocate new areas of memory, the address of which can go into a single pointer variable if you code int *ptr = malloc(100000*sizeof(int);

in C or int* ptr = new int[100000];

in C ++ ....

A variable (in C or C ++) is a source code object that has a name (like ptr

or x000002

or array

in this answer) and scope . At runtime, only objects are present (and variables do not exist). Read about memory addresses (which is practically located).

To have many variables, you need to have, for example, a huge source file:

int x000001;
int x000002;

      

etc. You can probably generate (with some other program) such a huge C or C ++ source file like. before:

////etc
int x999998;
int x999999;

      

But even if you create four billion lines of C source file, you won't have the patience to compile it. And if you did, the compilation would surely fail (at least at link time, which I consider to be part of the overall compilation of your program).

Note that the array declaration only defines one variable:



/// one single variable, but a huge one
int array[4294967296];

      

declares one variable named array

. Again, this will not compile and link (and if the variable is local inside some function, you will get at least a runtime stack overflow ). Typical call stacks are limited to one or more megabytes (depending on the operating system and computer).

Look at the picture in the wikipage virtual address space and understand that the aliasing pointer means there is virtual memory .

In practice, on a 32-bit computer, the virtual address space is often limited, for example, 3Gigabytes for a given process (each process starts some executable and has its own virtual address space). Detailed information about a specific operating system. On Linux, use setrlimit (2) - perhaps via an inline ulimit

from your bash shell to lower this limit.

In Linux, heap allocation is ( malloc

or new

) based on system calls that change the virtual address space, especially mmap (2) . Such calls can fail (and then malloc

fail, returning NULL

, but new

throw an exception), and on a 32-bit system they will fail up to 3Gbytes. You probably want to disable memory overflow .

If you are using Linux system read proc (5) and try

cat /proc/self/maps
cat /proc/$$/maps

      

then understand what their conclusion is. You should probably also read Advanced Linux Programming .

I recommend reading for a few days: Operating Systems: Three Simple Pieces (Free Download).

(on Windows or MacOSX or Android both malloc

and new

also use some operating system primitive to increase the virtual address space. I'll leave you to find which)

+3


source


It depends exactly on how you are trying to do it. It can crash, it can return an error, it can throw an exception.



+4


source


If your particular process tries to exceed the size of the process's virtual address space, it will simply run out of memory. What happens is what usually happens when your process runs out of memory. The memory allocation function will return a null pointer or something. Moreover, in theory, running out of address space is the only way to "drain memory" in an OS running virtual memory with swapping capabilities (real life is a little more complicated, but generally true).

As with all processes in the system ... your question is wrong. The OS, even if it is a 32-bit OS, is in no way limited to one 32-bit address space for all processes. For all practical tools and tasks, the OS can support a virtually unlimited number of concurrent independent 32-bit address spaces for different processes.

+3


source


If you create many variables with static storage time or automatic variables in a single block, the compiler or linker will most likely fail to create the executable.

If you create many automatic variables in many functions and activate all of them at the same time, the program will crash due to.

If you try to allocate that many bytes from dynamic storage, allocation will fail.

+2


source


when the limit is reached, virtual allocations that commit memory do not work. This means that even a standard 32-bit process can fail in virtual memory allocation. Maybe this link can help you: Pushing Windows Limits: Virtual Memory

+1


source







All Articles