Windows API one-day training (77) virtualalloc Function

Source: Internet
Author: User

The last time I learned about the global memory allocation, in windows, the memory management is divided into two parts. The global memory is the memory managed by the system, so all processes can access the memory, each process has its own memory space, which is the virtual memory space. The virtual memory space is large. When the physical memory is insufficient, the system will save the virtual memory data to the hard disk, so that as long as the hard disk space is large enough, each process can use 3 GB memory. Virtual Memory allocation can be used Program The main way to allocate memory in, such as a large amount of data buffer, Dynamic Allocation of memory space. Using the virtualalloc function to allocate memory is faster than the global memory.

The virtualalloc function declaration is as follows:

Winbaseapi
_ Bcount (dwsize)
Lpvoid
Winapi
Virtualalloc (
_ In_opt lpvoid lpaddress,
_ In size_t dwsize,
_ In DWORD flallocationtype,
_ In DWORD flprotect
);
Lpaddress is the address starting with the specified memory.
Dwsize indicates the size of the allocated memory.
Flallocationtype is the type of memory allocated.
Flprotect is the permission to access the allocated memory.

An example of calling a function is as follows:
#001 //
#002 // allocate virtual memory.
#003 // Cai junsheng 2007/11/20 QQ: 9073204 Shenzhen
#004 void memvirtual (void)
#005 {
#006 //
#007 // allocate the new memory size.
#008 uint nnewsize = (uint) Ceil (1500/1024 .0) * 1024;
#009 pbyte pnewbuffer = (pbyte) virtualalloc (null, nnewsize, mem_commit, page_readwrite );
#010 if (pnewbuffer)
#011 {
#012 // test the virtual memory.
#013 zeromemory (FIG, 1500 );
#014 memcpy (pnewbuffer, _ T ("virtual memory allocated successfully \ r \ n "),
#015 sizeof (_ T ("virtual memory allocated successfully \ r \ n ")));
#016 outputdebugstring (lpwstr) pnewbuffer );
#017
#018 // Delete the allocated memory.
#019 virtualfree (pnewbuffer, 0, mem_release );
#020}
#021
#022}

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/caimouse/archive/2007/11/20/1895301.aspx

Related Article

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: info-contact@alibabacloud.com 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.