Differences between globalalloc, malloc, and new

Source: Internet
Author: User

 

Globalalloc

It is reserved for compatibility with Win16. It is not used in Win32. Global memory objects are allocated using the globalalloc function. In the Windows 3. x era, two types of memory can be allocated: global and local, such as globalalloc and localalloc. However, in the Win32 era, these functions have been deprecated. Currently, there is only one kind of memory, namely, virtual memory. In Win32, all processes use memory areas that are isolated from each other. Each process has its own address space. In addition, the system uses the Page Swap function to simulate RAM by using disk space. Data in Ram will be exchanged to the disk when not in use, and will be reloaded into RAM when necessary.

Both are allocated memory areas on the heap.
Malloc

Is the dynamic memory allocation function in the C Runtime Library, WindowsProgramIt is basically not used because it has fewer features than the Windows Memory allocation function, such as organizing the memory.
Globalalloc () is an API used by a 16-bit windows program. It returns a memory handle. globallock () is used to obtain the memory zone. However, in 32-bit Windows systems, the new memory allocation function heapalloc () should be used for better support. globalalloc () can also be used mainly for compatibility.

Heapalloc apply memory from kernel32.dll
Globalalloc obsolete malloc apply memory Form C Runtime memory, and C r untime applys from kernel32.dll
New a wrapper of malloc but it is not a must for new to implement
Based on malloc.

Comemalloc apply memory from kernel32.dll

All are heap memory.

Recommend heapalloc for big block memory allocation
Recommend stack memory space.
Recommend heapalloc for big block memory allocation
Recommend stack memory space.

Malloc and free are standard library functions in C ++/C Language

New/delete is the c ++ Operator

both apply for dynamic memory and release memory.
for non-Internal data objects, the use of maloc/free alone cannot meet the requirements of dynamic objects. The constructor must be automatically executed when the object is created, and the Destructor must be automatically executed before the object is extinct. Since malloc/free is a library function rather than an operator and is not controlled by the compiler, it is impossible to impose the tasks of executing constructor and destructor on malloc/free.
therefore, the C ++ language requires a new operator that can complete dynamic memory allocation and initialization, and a delete operator that can clean up and release memory. Note that new/delete is not a database function.
let's first take a look at how malloc/free and new/delete implement dynamic memory management for objects. See example 7-8.

class OBJ {<br/> public: <br/> OBJ () {cout <"initialization" <Endl ;}< br/> ~ OBJ () {cout <"Destroy" <Endl ;}< br/> void initialize () {cout <"initialization" <Endl ;} <br/> void destroy () {cout <"Destroy" <Endl ;}< br/>}; <br/> void usemallocfree () {<br/> OBJ * A = (OBJ *) malloc (sizeof (OBJ); // apply for dynamic memory <br/> A-> initialize (); // initialization <br/> //... <Br/> A-> destroy (); // clear the job <br/> free (); // release memory <br/>}< br/> void usenewdelete () {<br/> OBJ * A = new OBJ; // apply for dynamic memory and initialize it <br/> //... <Br/> Delete A; // clear and release the memory <br/>}

Example 7-8 How to Use malloc/free and new/Delete to manage dynamic memory of Objects

The class OBJ function initialize simulates the constructor function, and the function destroy simulates the destructor function. In usemallocfree, because malloc/free cannot execute constructor and destructor, you must call the member functions initialize and destroy to complete initialization and clearing. The usenewdelete function is much simpler.
Therefore, we should not attempt to use malloc/free to manage the memory of dynamic objects. We should use new/Delete. Because the internal data type "object" does not have a process of construction and analysis, malloc/free and new/delete are equivalent to them.
Since the new/delete function completely covers malloc/free, why does C ++ not eliminate malloc/free? This is because C ++ programs often call C functions, and C Programs can only use malloc/free to manage dynamic memory.
If you use free to release the "New Dynamic Object", this object may cause program errors because it cannot execute the destructor. If you use Delete to release the "dynamic memory applied by malloc", theoretically, the program will not go wrong, but the program is poorly readable. Therefore, new/delete must be paired, and the same applies to malloc/free.
Global memory objects are allocated using the globalalloc function. In the Windows 3. x era, two types of memory can be allocated: global and local, such as globalalloc and localalloc. However, in the Win32 era, these functions have been deprecated. Currently, there is only one kind of memory, namely, virtual memory. In Win32, all processes use memory areas that are isolated from each other. Each process has its own address space. In addition, the system uses the Page Swap function to simulate RAM by using disk space. Data in Ram will be exchanged to the disk when not in use, and will be reloaded into RAM when necessary.

 

 

 

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.