内存分配/解除分配? [英] Memory Allocation/Deallocation?

查看:35
本文介绍了内存分配/解除分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在研究内存分配,但对基础知识有些困惑.我一直无法理解这些简单的东西.分配内存是什么意思?发生什么了?我希望能回答以下任何问题:

I have been looking at memory allocation lately and I am a bit confused about the basics. I haven't been able to wrap my head around the simple stuff. What does it mean to allocate memory? What happens? I would appreciated answers to any of these questions:

  1. 正在分配的内存"在哪里?
  2. 这个记忆"是什么?数组中的空间?或者是其他东西?
  3. 当这个内存"被分配时会发生什么?
  4. 当内存被释放时会发生什么?
  5. 如果有人能回答 malloc 在这些 C++ 行中做了什么,那也真的对我有帮助:

  1. Where is the "memory" that is being allocated?
  2. What is this "memory"? Space in an array? Or something else?
  3. What happens exactly when this "memory" gets allocated?
  4. What happens exactly when the memory gets deallocated?
  5. It would also really help me if someone could answer what malloc does in these C++ lines:

char* x; 
x = (char*) malloc (8);

谢谢.

推荐答案

内存模型

C++ 标准有一个内存模型.它试图以通用方式对计算机系统中的内存进行建模.标准定义字节是内存模型中的存储单元,内存由字节组成(第 1.7 节):

The Memory Model

The C++ standard has a memory model. It attempts to model the memory in a computer system in a generic way. The standard defines that a byte is a storage unit in the memory model and that memory is made up of bytes (§1.7):

C++ 内存模型中的基本存储单元是字节.[...] C++ 程序可用的内存由一个或多个连续字节序列组成.

The fundamental storage unit in the C++ memory model is the byte. [...] The memory available to a C++ program consists of one or more sequences of contiguous bytes.

对象模型

标准总是提供一个对象模型.这指定一个对象是一个存储区域(因此它由字节组成并驻留在内存中)(第 1.8 节):

The Object Model

The standard always provides an object model. This specifies that an object is a region of storage (so it is made up of bytes and resides in memory) (§1.8):

C++ 程序中的构造创建、销毁、引用、访问和操作对象.一个对象是一个存储区域.

The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is a region of storage.

所以我们开始了.内存是存储对象的地方.要将对象存储在内存中,必须分配所需的存储区域.

So there we go. Memory is where objects are stored. To store an object in memory, the required region of storage must be allocated.

标准提供了两个隐式声明的全局作用域分配函数:

The standard provides two implicitly declared global scope allocation functions:

void* operator new(std::size_t);
void* operator new[](std::size_t);

这些是如何实施的不是标准所关心的.重要的是他们应该返回一个指向某个存储区域的指针,其字节数对应于传递的参数(第 3.7.4.1 节):

How these are implemented is not the standard's concern. All that matters is that they should return a pointer to some region of storage with the number of bytes corresponding to the argument passed (§3.7.4.1):

分配函数尝试分配请求的存储量.如果成功,它将返回存储块的起始地址,该存储块的字节长度至少应与请求的大小一样大.从分配函数返回时,对分配存储的内容没有限制.

The allocation function attempts to allocate the requested amount of storage. If it is successful, it shall return the address of the start of a block of storage whose length in bytes shall be at least as large as the requested size. There are no constraints on the contents of the allocated storage on return from the allocation function.

它还定义了两个对应的释放函数:

It also defines two corresponding deallocation functions:

void operator delete(void*);
void operator delete[](void*);

定义为释放先前分配的存储(第 3.7.4.2 节):

Which are defined to deallocate storage that has previously been allocated (§3.7.4.2):

如果标准库中释放函数的参数是一个不是空指针值的指针(4.10),释放函数应释放指针所引用的存储,使所有指向任何部分的指针无效解除分配的存储.

If the argument given to a deallocation function in the standard library is a pointer that is not the null pointer value (4.10), the deallocation function shall deallocate the storage referenced by the pointer, rendering invalid all pointers referring to any part of the deallocated storage.

newdelete

通常,您不需要直接使用分配和释放函数,因为它们只会给您未初始化的内存.相反,在 C++ 中,您应该使用 newdelete 来动态分配对象.new-expression 通过使用上述分配函数之一获得所请求类型的存储空间,然后以某种方式初始化该对象.例如 new int() 将为 int 对象分配空间,然后将其初始化为 0.参见 §5.3.4:

new and delete

Typically, you should not need to use the allocation and deallocation functions directly because they only give you uninitialised memory. Instead, in C++ you should be using new and delete to dynamically allocate objects. A new-expression obtains storage for the requested type by using one of the above allocation functions and then initialises that object in some way. For example new int() will allocate space for an int object and then initialise it to 0. See §5.3.4:

new 表达式通过调用分配函数(3.7.4.1)来获取对象的存储空间.

A new-expression obtains storage for the object by calling an allocation function (3.7.4.1).

[...]

创建类型 T 的对象的 new-expression 初始化该对象 [...]

A new-expression that creates an object of type T initializes that object [...]

在相反的方向,delete 将调用对象的析构函数(如果有),然后释放存储空间(第 5.3.5 节):

In the opposite direction, delete will call the destructor of an object (if any) and then deallocate the storage (§5.3.5):

如果delete-expression 的操作数的值不是空指针值,delete-expression 将调用对象的析构函数(如果有)或者被删除的数组元素.

If the value of the operand of the delete-expression is not a null pointer value, the delete-expression will invoke the destructor (if any) for the object or the elements of the array being deleted.

[...]

如果delete-expression 的操作数的值不是空指针值,delete-expression 将调用释放函数(3.7.4.2).

If the value of the operand of the delete-expression is not a null pointer value, the delete-expression will call a deallocation function (3.7.4.2).

其他分配

然而,这些并不是分配或取消分配存储的唯一方式.该语言的许多结构都隐含地要求分配存储空间.例如,给出一个对象定义,如 int a;,也需要存储(第 7 节):

Other Allocations

However, these are not the only ways that storage is allocated or deallocated. Many constructs of the language implicitly require allocation of storage. For example, giving an object definition, like int a;, also requires storage (§7):

定义会导致保留适当数量的存储并完成任何适当的初始化 (8.5).

A definition causes the appropriate amount of storage to be reserved and any appropriate initialization (8.5) to be done.

C 标准库:mallocfree

此外, 头文件引入了 stdlib.h C 标准库的内容,其中包括 mallocfree 功能.它们也被 C 标准定义为分配和释放内存,很像 C++ 标准定义的分配和释放函数.下面是 malloc 的定义(C99 §7.20.3.3):

C standard library: malloc and free

In addition, the <cstdlib> header brings in the contents of the stdlib.h C standard library, which includes the malloc and free functions. They are also defined, by the C standard, to allocate and deallocate memory, much like the allocation and deallocation functions defined by the C++ standard. Here's the definition of malloc (C99 §7.20.3.3):

void *malloc(size_t size);
说明
malloc 函数为其大小由 size 指定的对象分配空间,并且其值不确定.
退货
malloc 函数返回一个空指针或一个指向分配空间的指针.

void *malloc(size_t size);
Description
The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate.
Returns
The malloc function returns either a null pointer or a pointer to the allocated space.

以及free的定义(C99 §7.20.3.2):

And the definition of free (C99 §7.20.3.2):

void free(void *ptr);
说明
free 函数使 ptr 指向的空间被释放,即可供进一步分配.如果 ptr 是空指针,则不会发生任何操作.否则,如果参数与之前由 callocmallocrealloc 函数返回的指针不匹配,或者如果空间已被释放通过调用 freerealloc,行为未定义.

void free(void *ptr);
Description
The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.

然而,在 C++ 中使用 mallocfree 从来都不是一个很好的借口.如前所述,C++ 有自己的替代品.

However, there's never a good excuse to be using malloc and free in C++. As described before, C++ has its own alternatives.

所以直接回答你的问题:

So to answer your questions directly:

  1. 正在分配的内存"在哪里?

  1. Where is the "memory" that is being allocated?

C++ 标准不在乎.它只是说程序有一些由字节组成的内存.可以分配此内存.

The C++ standard doesn't care. It simply says that the program has some memory which is made up of bytes. This memory can be allocated.

这个记忆"是什么?数组中的空间?或者是其他东西?

What is this "memory"? Space in an array? Or something else?

就标准而言,内存只是一个字节序列.这是故意非常通用的,因为该标准仅尝试建模典型的计算机系统.大多数情况下,您可以将其视为计算机 RAM 的模型.

As far as the standard is concerned, the memory is just a sequence of bytes. This is purposefully very generic, as the standard only tries to model typical computer systems. You can, for the most part, think of it as a model of the RAM of your computer.

当这个内存"被分配时会发生什么?

What happens exactly when this "memory" gets allocated?

分配内存使某些存储区域可供程序使用.对象在分配的内存中初始化.您只需要知道您可以分配内存.物理内存的实际分配往往由操作系统完成.

Allocating memory makes some region of storage available for use by the program. Objects are initialized in allocated memory. All you need to know is that you can allocate memory. The actual allocation of physical memory to your process tends to be done by the operating system.

当内存被释放时会发生什么?

What happens exactly when the memory gets deallocated?

取消分配一些先前分配的内存会导致该内存对程序不可用.它变成了解除分配的存储.

Deallocating some previously allocated memory causes that memory to be unavailable to the program. It becomes deallocated storage.

如果有人能回答 malloc 在这些 C++ 行中做了什么,那也真的对我有帮助:

It would also really help me if someone could answer what malloc does in these C++ lines:

char* x; 
x = (char*) malloc (8);

这里,malloc 只是分配 8 字节的内存.它返回的指针被转换为 char* 并存储在 x 中.

Here, malloc is simply allocating 8 bytes of memory. The pointer it returns is being cast to a char* and stored in x.

这篇关于内存分配/解除分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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