c - malloc() 5GB memory on a 32 bit machine -


i reading in book:

the virtual address space of process on 32 bit machine 2^32 i.e. 4gb of space. , every address seen in program virtual address. 4gb of space further goes through user/kernel split 3-1gb.

to better understand this, did malloc() of 5gb space , tried print addresses. if print addresses, how application going print whole 5gb address when has 3gb of virtual address space? missing here?

malloc() takes size_t argument. on 32 bit system it's alias unsigned 32 bit integer type. means cannot pass value bigger 2^32-1 argument malloc() making impossible request allocation of more 4gb of memory using function.

the same true other functions can used allocate memory. end either brk() or mmap syscall. length argument of mmap() of type ssize_t in case of brk() have provide pointer new end of allocated space. pointer again 32 bit.

so there absolutely no way tell kernel more 4gb of memory allocated 1 call) , it's not accident - wouldn't make sense anyway.

now it's true several calls malloc or other function allocates memory, requesting more 4gb in total. if try this, subsequent call (that cause extending allocated memory more 3gb) fail there no address space available.

so guess either didn't check malloc return value or did try run code (or similar):

int main() {     assert(malloc(5*1<<30)); } 

and assumed succeeded in allocating 5gb without verifying argument overflowed , instead of requesting 5368709120 bytes, requested 1073741824. 1 example verify on linux use:

$ ltrace ./a.out __libc_start_main(0x804844c, 1, 0xbfbcea74, 0x80484a0, 0x8048490 <unfinished ...> malloc(1073741824) = 0x77746008 $


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -