size of dynamically allocated array

后端 未结 4 2072
醉话见心
醉话见心 2020-12-09 10:54

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

4条回答
  •  难免孤独
    2020-12-09 11:19

    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 :-/

提交回复
热议问题