Finding size of dynamically allocated array

前端 未结 7 603
礼貌的吻别
礼貌的吻别 2020-12-10 11:08

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

7条回答
  •  旧巷少年郎
    2020-12-10 11:31

    why not a bit of extra info like this:

    template  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;
    };
    

提交回复
热议问题