删除所使用的已分配内存的计数到底在内存中的哪个位置?

发布于 2024-11-14 03:36:47 字数 157 浏览 3 评论 0原文

int* Array;

Array = new int[10];

delete[] Array;

delete 知道分配的内存数量。我在谷歌上搜索到它将它存储在内存中,但它依赖于编译器。无论如何可以使用获取这个计数吗?

int* Array;

Array = new int[10];

delete[] Array;

The delete knows the count of allocated memory. I Googled that it stores it in memory, but it's compiler dependent. Is there anyway to use get this count?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

秋心╮凉 2024-11-21 03:36:47

实际上,堆知道每次分配的大小。但是,这不是您可以轻松访问的东西,并且只能保证大于或等于请求的金额。有时,为了字节对齐的目的,会分配更多的空间。

正如 Ben 所说,在某些情况下,实现确实知道数组中有多少对象,以便可以调用它们的析构函数。

Actually, the heap knows how large each allocation is. However, that's not something that you can access easily, and it is only guaranteed to be greater than or equal to the amount requested. Sometimes more is allocated for the benefit of byte alignment.

As Ben said though, the implementation does know in certain circumstances how many objects are in the array so that their destructors can be called.

檐上三寸雪 2024-11-21 03:36:47

构建后没有检索元素数量的标准方法。事实上,对于 int 数组来说,它可能没有存储在任何地方。仅对于具有重要析构函数的元素数组才需要计数,以便删除[] 可以调用正确数量的析构函数。在您的示例中,没有任何析构函数调用,因为 int 没有析构函数。

There is no standard way to retrieve the number of elements after construction. In fact, for an int array, it is probably NOT stored anywhere. The count is only necessary for arrays of elements with non-trivial destructors, so that delete[] can call the right number of destructors. In your example, there aren't any destructor calls, since int doesn't have a destructor.

吾性傲以野 2024-11-21 03:36:47

分配动态分配的数组后,无法获取其元素数量。


统治它们的唯一方法

但是,您可以预先存储它:

int* Array;
size_t len = 10;
Array = new int[len];
delete[] Array;

自定义

如果您不喜欢这样,您可以创建自己的类:

class IntArray
{
public:
    int* data;
    size_t length;
    IntArray(size_t);
    ~IntArray();
};

IntArray::IntArray(size_t len)
{
    length = len;
    data = new int[len];
}

IntArray::~IntArray()
{
    length = 0;
    delete data;
    data = NULL;
}

std::vector

我推荐的方法是使用 std::vector< /code>

std::vector<int> Array (10, 0);

您可以像常规数组一样使用它......具有额外功能:

for(size_t i = 0; i < Array.size(); ++i)
    Array[i] = i;

There's no way to get the number of elements of a dynamically allocated array after you allocate it.


The one way to rule them all

However, you can store it beforehand:

int* Array;
size_t len = 10;
Array = new int[len];
delete[] Array;

Custom class

If you don't like that, you could create your own class:

class IntArray
{
public:
    int* data;
    size_t length;
    IntArray(size_t);
    ~IntArray();
};

IntArray::IntArray(size_t len)
{
    length = len;
    data = new int[len];
}

IntArray::~IntArray()
{
    length = 0;
    delete data;
    data = NULL;
}

std::vector

The method I recommend is to use std::vector:

std::vector<int> Array (10, 0);

You can use it just like a regular array... with extra features:

for(size_t i = 0; i < Array.size(); ++i)
    Array[i] = i;
嘿嘿嘿 2024-11-21 03:36:47

此类分配中的元素数量可能有一两个计数,具体取决于您正在使用的类型和实现,尽管您无法真正按照您可能想要的方式访问它们。

第一个是您正在使用的实际内存管理器(提供 malloc 的库)存储的记帐信息。它将存储已在系统的空闲存储中分配的某个大小的记录(例如,使用 glibc malloc 可以进行堆或匿名内存分配)。该空间至少与您尝试存储的数据一样大(sizeof(int)*count+delta,其中 delta 是我在下面讨论的 C++ 编译器的跟踪信息),但它也可能更大,甚至更大。

第二个计数是编译器保存的一个值,它告诉它如何对数组中的所有元素调用析构函数(RAII 的全部魔力),但该值不可访问,甚至可以在不直接存储信息的情况下完成,尽管那不太可能。

正如其他人所说,如果您需要跟踪有关分配大小的信息,您可能想要使用向量,您甚至可以将其用作实际数组,以用于指针数学(如果需要)(请参阅 http://www.cplusplus.com/reference/stl/vector/ 了解更多信息)。

There are likely one or two counts of the number of elements in such an allocation depending upon the type and the implementation that you are using though you can't really access them in the way you probably want.

The first is the accounting information stored by the actual memory manager that you are using (the library that provides malloc). It will store that a record of some size has been allocated in the free store of the system (heap or anonymous memory allocation are both possible with the glibc malloc for example). This space will be at least as large as the data you are trying to store (sizeof(int)*count+delta where delta is the C++ compiler's tracking information I talk about below), but it could also be larger, even significantly so.

The second count is a value kept by the compiler that tells it how to call destructors on all the elements in the array (the whole magic of RAII), but that value is not accessible and could probably even be done without directly storing the information, though that would be unlikely.

As others have said, if you need to track the information on allocation size you probably want to use a vector, you can even use it as an actual array for the purpose of pointer math if need be (see http://www.cplusplus.com/reference/stl/vector/ for more on this).

╭⌒浅淡时光〆 2024-11-21 03:36:47

谁说真的有一个?

这些东西取决于实现,因此对你、我或任何想知道它的人来说都是无趣的。

Who says that there actually is one?

This stuff depends on the implementation and as such is uninteresting for you, me or whoever wants to know it.

世界等同你 2024-11-21 03:36:47

C++ 通常故意不允许您访问该信息,因为数组是简单类型,不会将该信息与其关联。最终,这些信息必须被存储,但编译器可以自由地确定 C++ 标准如何、在何处以及何时存储,以允许在程序集中进行优化。

基本上,要么自己将其存储在某个地方,要么(大多数情况下更好)使用 std::vector 。

C++ generally intentionally doesn't allow you access to that information, because arrays are simple types that do not keep that information associated with them. Ultimately that information must be stored, but the compiler is free to figure out how, where, and when by the C++ standards to allow for optimization in the assembly.

Basically, either store it yourself somewhere, or (better, most of the time), use std::vector.

っ〆星空下的拥抱 2024-11-21 03:36:47

不,如果您需要知道,您需要自己跟踪。

如果 std::vector 大小不固定,许多人喜欢使用它。 std::vector 跟踪为您分配的大小。

No, you need to keep track of it yourself if you need to know.

Many people like using a std::vector if it's not a fixed size. std::vector keeps track of the size allocated for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文