gpt4 book ai didi

c++ - 内存分配/解除分配?

转载 作者:行者123 更新时间:2023-12-01 19:56:18 25 4
gpt4 key购买 nike

就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the help center为指导。




8年前关闭。




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

  • 正在分配的“内存”在哪里?
  • 这个“内存”是什么?数组中的空间?或者是其他东西?
  • 当这个“内存”被分配时究竟会发生什么?
  • 当内存被释放时究竟会发生什么?
  • 如果有人能回答 malloc 在这些 C++ 行中所做的事情,它也会真正帮助我:
    char* x; 
    x = (char*) malloc (8);

  • 谢谢你。

    最佳答案

    内存模型

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

    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 constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is a region of storage.



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

    分配和解除分配函数

    该标准提供了两个隐式声明的全局范围分配函数:
    void* operator new(std::size_t);
    void* operator new[](std::size_t);

    如何实现这些不是标准所关心的。重要的是他们应该返回一个指向某个存储区域的指针,其字节数对应于传递的参数(第 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.



    它还定义了两个对应的释放函数:
    void operator delete(void*);
    void operator delete[](void*);

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

    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 表达式通过使用上述分配函数之一获得所请求类型的存储空间,然后以某种方式初始化该对象。例如 new int()将为 int 分配空间对象,然后将其初始化为 0。参见 §5.3.4:

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

    [...]

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



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

    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.

    [...]

    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 节):

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



    C 标准库: mallocfree
    此外, <cstdlib> header 引入了 stdlib.h 的内容C 标准库,包括 mallocfree职能。它们也被 C 标准定义为分配和释放内存,很像 C++ 标准定义的分配和释放函数。这是 malloc的定义(C99 §7.20.3.3):

    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):

    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.



    然而,使用 malloc 从来都不是一个很好的借口。和 free在 C++ 中。如前所述,C++ 有自己的替代方案。

    问题解答

    所以直接回答你的问题:
  • 正在分配的“内存”在哪里?

    C++ 标准并不关心。它只是说程序有一些由字节组成的内存。可以分配此内存。
  • 这个“内存”是什么?数组中的空间?或者是其他东西?

    就标准而言,内存只是一个字节序列。这是故意非常通用的,因为该标准仅尝试对典型的计算机系统进行建模。大多数情况下,您可以将其视为计算机 RAM 的模型。
  • 当这个“内存”被分配时究竟会发生什么?

    分配内存使某些存储区域可供程序使用。对象在分配的内存中初始化。你只需要知道你可以分配内存。物理内存的实际分配往往由操作系统完成。
  • 当内存被释放时究竟会发生什么?

    取消分配一些先前分配的内存会导致该内存对程序不可用。它成为解除分配的存储。
  • 如果有人能回答 malloc 在这些 C++ 行中所做的事情,它也会真正帮助我:
    char* x; 
    x = (char*) malloc (8);

    在这里,malloc只是分配 8 个字节的内存。它返回的指针被强制转换为 char*并存储在 x .
  • 关于c++ - 内存分配/解除分配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15604411/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com