Delphi Deposit (ii)

Source: Internet
Author: User

The system gives the program the number of addresses is 4G, why is not 3G or 5G? Because the maximum value of a 32-bit pointer is $FFFFFFFF, it does not represent much more, the root of which is to return to the CPU's addressing capability, address bus, and so on.

Under Win64, the system addresses the program to 16EB (0-$FFFFFFFFFFFFFFFF), which is 1.,844,674,407,370,96e,+19. But Win64 has not been popularized yet, and we have to go back to the actual Win32.

In this 4G address, the system will be left half ($80000000-$FFFFFFFF, which 2 G is shared by each process) as a macro management; Only give program 2G (0-$7FFFFFFF).
In this 2G address, not all to the user, the low-end 0-$FFFF is used for null pointer assignment, prohibit access; The high-end $7fff0000-$7FFFFFFF is also left out as a critical section of the process and is also forbidden to access. In fact, the private space address of the process is only $10000-$7feffff.

The above result, we can be confirmed by the GetSystemInfo function, through the GetSystemInfo function can obtain a TSYSTEMINFO structure, lpminimumapplicationaddress and lpmaximum in the structure Applicationaddress represents the lowest and highest memory addresses that a program (or dynamic-link library) can access, respectively.

var si:tsysteminfo;  begin  GetSystemInfo (SI); Showmessagefmt ('%p-%p '{The result is: 00010000-7ffeffff}end;

An important memory-related parameter can also be obtained via GetSystemInfo: Page size (PageSize)

var si:tsysteminfo; begin {4096; 4096 bytes is 4K} End;

PageSize is the basic unit of System management memory, the reason why need to use GetSystemInfo to obtain different system PageSize will also have difference.

What we need to know is that the memory allocated with the VirtualAlloc function is the smallest unit in PageSize (4K); If we use VirtualAlloc to allocate memory to an integer (4 bytes), we will waste 4,092 bytes, which means that VirtualAlloc is not suitable for allocating small memory, so there are many functions to allocate memory.

Let's get down to the topic and learn about the stack.

When it comes to "stacks", the idea of "heap" and "stack" refers to "stacks"; Both "stack" and "heap" are a small segment of the memory area ($10000-$FFEFFFF) that the program can manipulate.

System functions have HeapReAlloc, GlobalAlloc and other functions of allocating "heap", but there is no function of allocating "stack", because "stack" is automatically managed by the program; Each program from its own range of available addresses as a "stack", the program can automatically adjust its size as needed, but we can set its maximum and minimum value. In Delphi, you can set it from:
Linker, Options, Project, [Min stack size and Max stack size]

The "stack" is used to stage local variables and function parameters, which are applied by the program when needed, and released when it is exhausted.

Because the "stack" space is generally not very large, so we generally do not make local variables too large (especially when using arrays);
Because access to the "stack" than access to the "heap" to the concise, fast, so as much as possible to use local variables, less use of global variables.

Delphi Deposit (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.