VirtualAlloc和HeapAlloc有什么区别? [英] What's the differences between VirtualAlloc and HeapAlloc?

查看:171
本文介绍了VirtualAlloc和HeapAlloc有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows环境中有很多分配内存的方法,例如VirtualAllocHeapAllocmallocnew.

There are lots of method to allocate memory in Windows environment, such as VirtualAlloc, HeapAlloc, malloc, new.

因此,它们之间有什么区别?

Thus, what's the difference among them?

推荐答案

每个API都有不同的用途.每一项还要求您在处理完内存后使用正确的释放/释放功能.

Each API is for different uses. Each one also requires that you use the correct deallocation/freeing function when you're done with the memory.

一个低级Windows API,提供了很多选项,但主要用于相当特定情况下的人们.只能在更大的块中分配内存(不是4KB).在某些情况下您需要它,但是当您处于这些情况之一时,您就会知道.最常见的一种是您是否必须直接与另一个进程共享内存.不要将其用于通用内存分配.使用VirtualFree取消分配.

A low-level, Windows API that provides lots of options, but is mainly useful for people in fairly specific situations. Can only allocate memory in (edit: not 4KB) larger chunks. There are situations where you need it, but you'll know when you're in one of these situations. One of the most common is if you have to share memory directly with another process. Don't use it for general-purpose memory allocation. Use VirtualFree to deallocate.

分配所需的任何内存大小,而不是VirtualAlloc的大块内存. HeapAlloc知道何时需要调用VirtualAlloc,并自动为您执行此操作.与malloc类似,但仅适用于Windows,并提供了多个其他选项.适用于分配常规内存块.某些Windows API可能会要求您使用它来分配传递给它们的内存,或者使用其随附的HeapFree释放它们返回给您的内存.

Allocates whatever size of memory you ask for, not in big chunks than VirtualAlloc. HeapAlloc knows when it needs to call VirtualAlloc and does so for you automatically. Like malloc, but is Windows-only, and provides a couple more options. Suitable for allocating general chunks of memory. Some Windows APIs may require that you use this to allocate memory that you pass to them, or use its companion HeapFree to free memory that they return to you.

分配内存的C方法.如果您使用C而不是C ++编写代码,并且希望代码可以在例如Unix计算机也是如此,或者有人专门说您需要使用它.不初始化内存.适用于分配常规内存块,例如HeapAlloc.一个简单的API.使用free取消分配. Visual C ++的malloc调用HeapAlloc.

The C way of allocating memory. Prefer this if you are writing in C rather than C++, and you want your code to work on e.g. Unix computers too, or someone specifically says that you need to use it. Doesn't initialise the memory. Suitable for allocating general chunks of memory, like HeapAlloc. A simple API. Use free to deallocate. Visual C++'s malloc calls HeapAlloc.

分配内存的C ++方法.如果您使用C ++编写,请首选此选项.它将一个或多个对象也放入分配的内存中.使用delete取消分配(或对于数组使用delete[]). Visual Studio的new调用HeapAlloc,然后可能根据调用方式初始化对象.

The C++ way of allocating memory. Prefer this if you are writing in C++. It puts an object or objects into the allocated memory, too. Use delete to deallocate (or delete[] for arrays). Visual studio's new calls HeapAlloc, and then maybe initialises the objects, depending on how you call it.

在最新的C ++标准(C ++ 11及更高版本)中,如果必须手动使用delete,说明您做错了,应该使用像unique_ptr这样的 smart指针 .从C ++ 14开始,对new可以说相同(已替换为诸如make_unique()之类的功能).

In recent C++ standards (C++11 and above), if you have to manually use delete, you're doing it wrong and should use a smart pointer like unique_ptr instead. From C++14 onwards, the same can be said of new (replaced with functions such as make_unique()).

还有一些其他类似功能,例如SysAllocString,可能会告诉您必须在特定情况下使用.

There are also a couple of other similar functions like SysAllocString that you may be told you have to use in specific circumstances.

这篇关于VirtualAlloc和HeapAlloc有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆