Finding size of dynamically allocated array

前端 未结 7 598
礼貌的吻别
礼貌的吻别 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:36

    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.

提交回复
热议问题