What is the max addressable memory space in a 32-bit C++ program?

走远了吗. 提交于 2021-01-28 08:39:44

问题


In debug mode I saw that the pointers have addresses like 0x01210040, but as I realized, 0x means hexadecimal right? And there're 8 hex digits, i.e. in total there're are 128 bits that are addressed?? So does that mean that for 32-bit system the first two digits are always 0, and for a 64-bit system the first digit is 0?

Also, may I ask that, for a 32-bit program, would I be able to allocate as much as 3GB of memory as long as I remain in the heap and use only malloc()? Or is there some limitations the Windows system poses on a single thread? (the IDE I'm using is VS2012)

Since actually I was running a 32-bit program in a 64-bit system, but the program crashed with a memory leak when it only allocated about 1.5GB of memory...and I can't seem to figure out why.

(Oooops...sorry guys I think I made a simple mistake with the first question...indeed one hex digit is 4 bits, and 8 makes 32bits. However here is another question...how is address represented in a 64-bit program?)


回答1:


For 32-bit Windows, the limit is actually 2GB usable per process, with virtual addresses from 0x00000000 (or simply 0x0) through 0x7FFFFFFF. The rest of the 4GB address space (0x80000000 through 0xFFFFFFFF) for use by Windows itself. Note that these have nothing to do with the actual physical memory addresses.

If your program is large address space aware, this limit is increased to 3GB on 32bit systems and 4GB for 32bit programs running on 64bit Windows.

  • http://msdn.microsoft.com/en-us/library/windows/desktop/aa366912(v=vs.85).aspx

And for the higher limits for large address space aware programs (IMAGE_FILE_LARGE_ADDRESS_AWARE), see here:

  • http://msdn.microsoft.com/en-us/library/aa366778.aspx

You might also want to take a look at the Virtual Memory article on Wikipedia to better understand how the mapping between virtual addresses and physical addresses works. The first MSDN link above also has a short explanation:

The virtual address space for a process is the set of virtual memory addresses that it can use. The address space for each process is private and cannot be accessed by other processes unless it is shared. A virtual address does not represent the actual physical location of an object in memory; instead, the system maintains a page table for each process, which is an internal data structure used to translate virtual addresses into their corresponding physical addresses. Each time a thread references an address, the system translates the virtual address to a physical address. The virtual address space for 32-bit Windows is 4 gigabytes (GB) in size and divided into two partitions: one for use by the process and the other reserved for use by the system. For more information about the virtual address space in 64-bit Windows, see Virtual Address Space in 64-bit Windows.


EDIT: As user3344003 points out, these values are not the amount of memory you can allocate using malloc or otherwise use for storing values, they just represent the size of the virtual address space.




回答2:


There are a number of limits that would restrict the size of your malloc allocation.

1) The number of bits, restricts the size of the address space. For 32-bits, that is 4B. 2) System the subdivide that for the various processor modes. These days, usually 2GB goes to the user and 2GB to the kernel. 3) The address space may be limited by the size of the page tables. 4) The total virtual memory may be limited by the size of the page file. 5) Before you start malloc'ing, there be stuff already in the virtual address space (e.g., code stack, reserved area, data). Your malloc needs to return a contiguous block of memory. Largest theoretical block it could return has to fit within unallocated areas of virtual memory. 6) Your memory management heap may restrict the size that can be allocated.

There probably other limitations that I have omitted.

-=-=-=-=-

If your program crashed after allocating 1.5GB through malloc, did you check the return value from malloc to see if it was not null?

-=-=-=-=-=

The best way to allocate huge blocks of memory is through operating system services to map pages into the virtual address space.---not using malloc.




回答3:


In reference to the following article

For a 32-bit application launched in a 32-bit Windows, the total size of all the mentioned data types must not exceed 2 Gbytes. The same 32-bit program launched in a 64-bit system can allocate about 4 Gbytes (actually about 3.5 Gbytes)

The practical data you are looking at is around 1.7 GB due to space occupied by windows.

By any chance how did you find out the memory it had allocated when it crashed.?



来源:https://stackoverflow.com/questions/25165021/what-is-the-max-addressable-memory-space-in-a-32-bit-c-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!