动态分配数组的大小

发布于 2024-08-17 11:27:35 字数 199 浏览 5 评论 0原文

分配给动态分配数组起始地址的指针是否没有数组大小的信息?所以我们必须使用另一个变量来存储它的大小,以便稍后通过指针处理数组。

但是当我们释放动态分配的数组时,我们不指定大小,而是只是“free ptr”或“delete [] ptr”。 free或delete怎么知道数组的大小呢?我们可以使用相同的方案来避免将数组的大小存储在另一个变量中吗?

谢谢!

Is it true that a pointer assigned to the starting address of a dynamically allocated array does not have the information of the size of the array? So we have to use another variable to store its size for later processing the array through the pointer.

But when we free the dynamically allocated array, we don't specify the size, instead we just "free ptr" or "delete [] ptr". How could free or delete know the size of the array? Can we use the same scheme to avoid storing the size of the array in another variable?

Thanks!

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

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

发布评论

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

评论(4

2024-08-24 11:27:35

是的,这是真的。

delete 知道内存块的大小,因为 new 向该块添加了额外的信息(通常在该区域返回给用户之前),其中包含其大小以及其他信息。请注意,这在很大程度上是特定于实现的,不应该由您的代码使用。

因此,回答你的最后一个问题: - 我们不能使用它 - 它是一个高度依赖于平台和编译器的实现细节。


例如,在 K&R2 中演示的示例内存分配器中,这是放置在每个已分配块之前的“标头”:

typedef long Align; /* for alignment to long boundary */

union header { /* block header */
  struct {
    union header *ptr; /* next block if on free list */
    unsigned size; /* size of this block */
  } s;

  Align x; /* force alignment of blocks */
};

typedef union header Header;

size 是已分配块的大小(然后由 free 使用),或删除)。

Yes, this is true.

delete knows the size of the memory chunk because new adds extra information to the chunk (usually before the area returned to the user), containing its size, along with other information. Note that this is all very much implementation specific and shouldn't be used by your code.

So to answer your last question: No - we can't use it - it's an implementation detail that's highly platform and compiler dependent.


For example, in the sample memory allocator demonstrated in K&R2, this is the "header" placed before each allocated chunk:

typedef long Align; /* for alignment to long boundary */

union header { /* block header */
  struct {
    union header *ptr; /* next block if on free list */
    unsigned size; /* size of this block */
  } s;

  Align x; /* force alignment of blocks */
};

typedef union header Header;

size is the size of the allocated block (that's then used by free, or delete).

太阳公公是暖光 2024-08-24 11:27:35

有趣的是,历史上它是delete [20] arr;,就像arr = new int[20]一样。然而实践证明,分配器可以轻松地存储有关大小的信息,并且由于大多数使用它的人都会存储它,因此它被添加到标准中。

更有趣且鲜为人知的是,这种“扩展删除语法”实际上受到一些 C++ 编译器的支持(尽管即使面对 C++98 标准也是不正确的),尽管没有一个编译器需要它。

int* arr = new int[20];
delete [20] arr;

然而,令人悲伤的是,没有符合标准的方法来检索传递的大小供您自己使用:-/

The funny thing is that historically it was delete [20] arr; just as it is arr = new int[20]. However practice proved that the information on size can be painlessly stored by the allocator, and since most people using it then stored it anyway, it was added to the standard.

What is more funny, and little known, is the fact that this "extended delete syntax" is in fact supported by a few C++ compilers (despite being incorrect even in face of the C++98 standard), although none require it.

int* arr = new int[20];
delete [20] arr;

The sad part about this all however is, that there's no standard-conforming way to retrieve that passed size for your own use :-/

尾戒 2024-08-24 11:27:35

确实,数组不包含数组的大小,您必须存储该信息以供以后使用。当通过deletefree删除数组时,它是指向您传递的已分配内存的指针。使用的内存管理器(由系统或您自己的自定义覆盖 new 和 delete)知道已释放的内存区域,并跟踪它。希望这是有道理的。

It is true that the array does not contain the size of the array, you have to store that information for later. When deleting an array through delete or free it is the pointer to the allocated memory you pass. The memory manager used (either by the system or your own custom from overriding new and delete) knows the memory area that is freed, and keeps track of it. Hope it makes sense.

メ斷腸人バ 2024-08-24 11:27:35

是的,这是真的。这就是为什么您很少尝试直接处理这个问题,而是使用标准容器的部分原因。处理这个问题唯一有意义的时候是您决定自己实现一个容器(在这种情况下,您通常会跟踪容器实现中的大小信息)。

Yes, it's true. This is part of why you should rarely try to deal with this directly, and use a standard container instead. About the only time it makes sense to deal with it is if you decide to implement a container yourself (in which case you'll normally track the size information in your container's implementation).

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