c++ - What happens when the number of possible virtual addresses are exceeded -


suppose i'm writing program environment has 32 bits virtual spaces addresses (4294967296 addresses), happens if create more 4294967296 variables, exceeding number of possible addresses? or programs in environment collectively use on 4294967296 addresses?

what happens if create more 4294967296 variables

i guess confused. don't create variables. practically use c dynamic memory allocation (using malloc & free , friends) or c++ dynamic memory allocation (e.g. dynamic memory management, above low level memory management ::operator new, which, in many implementations using malloc).

notice malloc (in c) , new (in c++) don't create fresh variables. allocating fresh memory zones address go 1 pointer variable, if code int *ptr = malloc(100000*sizeof(int); in c, or int* ptr = new int[100000]; in c++ ....

a variable (in c or c++) source code thing has name (e.g. ptr or x000002 or array in answer here) , scope. during execution, locations matter (and variables not exist). read memory addresses (what practically locations are).

so have many variables, you'll need have example huge source file with:

int x000001; int x000002; 

and on. can generate (with other program) such huge c or c++ source file, e.g. to:

////etc int x999998; int x999999; 

but if generate 4 billion lines of source c file, won't have patience compile it. , if did, compilation surely fail (at least @ link time, view part of overall compilation of program).

notice array declaration defines 1 variable:

/// 1 single variable, huge 1 int array[4294967296]; 

declares 1 variable. again, won't compile & link (and if variable local 1 inside function, you'll @ least stack overflow @ runtime). typical call stacks limited 1 or few megabytes (this operating system & computer dependent).

look @ picture in virtual address space wikipage , understand pointer aliasing means , virtual memory is.

in practice, on 32 bits computer, virtual address space limited e.g. 3gigabytes given process (each process running executable , has own virtual address space). details operating system specific. on linux, use setrlimit(2) - thru ulimit builtin of bash shell - lower limit.

on linux, dynamic memory allocation (malloc or new) based upon system calls modifying virtual address space, notably mmap(2). such calls can fail (and malloc fail returning null, , new raise exception), , on 32 bits system fail before 3gbytes. want disable memory overcommitment.

if happen use linux system, read proc(5) , try

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

then understand output. should read advanced linux programming.

i recommend taking several days read: operating systems : 3 easy pieces (it freely downloadable).

(on windows or macosx or android, both malloc & new using operating system primitive increase virtual address space. leave find ones)


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -