数组的大小(新[]) [英] Size of array (new[])

查看:100
本文介绍了数组的大小(新[])的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调试堆,我可以得到阵列,这是由创建的新尺寸[]

In debug heap I can get size of array, which was created by new[]:

int size = *((int *)((char *)ptr - 16));

这是正常工作,如果数组的大小小于28(但我不知道为什么?0_0)。

It is working correctly if the size of the array is less than 28 (but I don't know why? 0_0).

难道(不使用调试堆)这一招在释放模式工作?

Does this trick working in release mode (not using debug heap)?

我怎样才能得到数组的大小(100%的工作和稳定的)?

How I can get size of the array (100% working and stable)?

推荐答案

您是靠一个实现细节。这是你的特定实施如何存储其中阵列被放置在存储器区域的大小。因为你已经看到,存储器区域的大小可以比分配的数组的大小更大。

You are relying on an implementation detail. That's how your particular implementation stores the size of the memory region where the array is placed. As you already see, the size of the memory region may be bigger than the size of the allocated array.

如果您需要了解与分配的数组的大小新[] ,你必须保持该值左右。

If you need to know the size of an array allocated with new[], you'll have to keep that value around.

这篇关于数组的大小(新[])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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