【发布时间】:2015-08-02 18:05:19
【问题描述】:
我怎样才能知道动态分配数组的大小? 使用以下方法的普通数组可以正常工作,但我不能对动态分配的数组做同样的事情。请查看并感谢您的帮助。
#include <iostream>
using namespace std;
int main() {
//normal array
int array[5];
cout << sizeof(array)/sizeof(array[0]) << endl; //this outputs the correct size
//dynamically allocated array
int *dArray = new int[5];
//how to calculate and output the size here?
return 0;
}
【问题讨论】:
-
[这里有一个类似的问题。或者你可以只使用 std::vector。 (:][1] [1]:stackoverflow.com/questions/2034450/…