什么是C&中的原始内存? C ++? [英] What is raw memory in C & C++?
问题描述
很抱歉提出这个问题,但实际上我对此一无所知. 我已阅读以下常见问题解答条目: 我可以释放分配有new的指针吗?我可以删除通过malloc()分配的指针吗?
Sorry for asking this question, but actually I don't know about this. I have read in the following FAQ entry: Can I free() pointers allocated with new? Can I delete pointers allocated with malloc()?
此外,不能保证new和delete用于获取和释放原始内存的机制与
malloc()
和free()
兼容.
我只想知道什么是原始内存"?
I just want to know that what is this "raw memory"?
推荐答案
原始内存"指的是内存块,被视为字节的非结构化数组.更高级别的语言将它们用作对象的存储.该程序通常与这些对象进行交互,而不是与低级字节值进行交互.
"Raw memory" refers to blocks of memory, treated as unstructured arrays of bytes. Higher-level languages use these as the storage for objects; the program usually interacts with these objects, not the low-level byte values.
在C ++中,可以使用两个不同的分配函数动态分配原始内存:
In C++, raw memory can be allocated dynamically using two different allocation functions:
-
operator new
,用于在使用new
创建对象时从免费商店进行分配
C库中的 -
malloc
operator new
, used to allocate from the free store when you create an object usingnew
malloc
from the C library
由new
分配的内存必须与delete
一起释放;并且用malloc
分配的内存必须用free
释放.
Memory allocated by new
must be released with delete
; and memory allocated with malloc
must be released with free
.
您引用的行说明了这些可能使用不同的机制来管理分配.因此使用错误的函数来释放内存是错误的(例如,使用new
分配内存并尝试使用free
释放内存).
The line you quote explains that these might use different mechanisms to manage allocation; so that it's an error to use the wrong function to release memory (e.g. to allocate memory with new
and try to release it with free
).
这篇关于什么是C&中的原始内存? C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!