Finding size of dynamically allocated array

2019-02-16 05:05发布

Why is it not possible to get the length of a buffer allocated in this fashion.

AType * pArr = new AType[nVariable];

When the same array is deallocated

delete [] pArr;

the runtime must know how much to deallocate. Is there any means to access the length before deleting the array. If no, why no such API is provided that will fetch the length?

7条回答
一纸荒年 Trace。
2楼-- · 2019-02-16 05:37

Is there any means to access the length before deleting the array?

No. there is no way to determine that.
The standard does not require the implementation to remember and provide the specifics of the number of elements requested through new.
The implementation may simply insert specific bit patterns at end of allocated memory blocks instead of remembering the number of elements, and might simply lookup for the pattern while freeing the memory.
In short it is solely an imlpementation detail.


On a side note, There are 2 options to practically overcome this problem:

  1. You can simple use a std::vector which provides you member functions like size() or
  2. You can simply do the bookkeeping yourself.

new atleast allocates enough memory as much as you requested.
You already know how much memory you requested so you can calculate the length easily. You can find size of each item using sizeof.

Total memory requested / Memory required for 1 item = No of Items
查看更多
仙女界的扛把子
3楼-- · 2019-02-16 05:38

No, not really. At least not in a platform-independent, defined way.

Most implementations store the size of a dynamically allocated array before the actual array though.

查看更多
老娘就宠你
4楼-- · 2019-02-16 05:43

The runtime must deallocate the same amount as it allocated, and it does keep track of this in some manner (usually very indirectly). But there's no reliable way of getting from amount allocated to number of elements: the amount allocated cannot be less than the number of elements times the size of each element, but it will often be more. Alignment considerations, for example, mean that new char[5] and new char[8] will often allocate the same amount of memory, and there are various allocation strategies which can cause significantly more memory to be allocated that what is strictly necessary.

查看更多
来,给爷笑一个
5楼-- · 2019-02-16 05:44

There is no portable way in C++ to get the size of a dynamically allocated array from the raw pointer.

Under MSVC and WIN32 you can get the size of the allocated block with the _msize(void*) function.

see https://msdn.microsoft.com/en-us/library/z2s077bc.aspx for further details.

查看更多
叛逆
6楼-- · 2019-02-16 05:50

why not a bit of extra info like this:

template <typename T> class AType
{
public:

    AType(size_t s) : data(0)
    {
        a_size = s;
        data = new T[s];
    }
    ~AType() {
        if (data != nullptr)
            delete [] data;
    }

    size_t getSize() const
    {
        return a_size * sizeof(T);
    }

private:
    size_t a_size;
    T* data;
};
查看更多
叼着烟拽天下
7楼-- · 2019-02-16 05:51

The runtime DOES know how much was allocated. However such details are compiler specific so you don't have any cross platform way to handle it.

If you would like the same functionality and be able to track the size you could use a std::vector as follows:

std::vector< AType > pArr( nVariable );

This has the added advantage of using RAII as well.

查看更多
登录 后发表回答