C++ 如何找到动态分配数组的大小(使用sizeof())?

C++ 如何找到动态分配数组的大小(使用sizeof())?,c++,arrays,C++,Arrays,如何确定动态分配阵列的大小? 对于普通数组,使用下面的方法可以很好地工作,但对于动态分配的数组,我不能做同样的事情。请看一看,谢谢你的帮助 #include <iostream> using namespace std; int main() { //normal array int array[5]; cout << sizeof(array)/sizeof(array[0]) << endl; //this outputs th

如何确定动态分配阵列的大小? 对于普通数组,使用下面的方法可以很好地工作,但对于动态分配的数组,我不能做同样的事情。请看一看,谢谢你的帮助

#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;
int main(){
//标准阵列
int数组[5];
cout不可能以可移植的方式(从新的
获取实际分配的大小)

您可以考虑定义自己的代码>::运算符new <代码>,但我不建议这样做。

你应该更多地使用和学习C++。

< P>不可能(以一种可移植的方式从代码<新代码/代码>中获得真正的分配大小)。

您可以考虑定义自己的代码>::运算符new <代码>,但我不建议这样做。

你应该更多地使用和学习C++。

< P>不可能(以一种可移植的方式从代码<新代码/代码>中获得真正的分配大小)。

您可以考虑定义自己的代码>::运算符new <代码>,但我不建议这样做。

你应该更多地使用和学习C++。

< P>不可能(以一种可移植的方式从代码<新代码/代码>中获得真正的分配大小)。

您可以考虑定义自己的代码>::运算符new <代码>,但我不建议这样做。


你应该更多地使用和学习C++。

< P>你不能计算动态数组的大小,所以需要明确地提供数组的大小。< /P>
#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 size = 5; // array size
    int *dArray = new int[size];


    return 0;
}
#包括
使用名称空间std;
int main(){
//标准阵列
int数组[5];

cout无法计算动态数组的大小,因此需要明确提供数组的大小

#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 size = 5; // array size
    int *dArray = new int[size];


    return 0;
}
#包括
使用名称空间std;
int main(){
//标准阵列
int数组[5];

cout无法计算动态数组的大小,因此需要明确提供数组的大小

#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 size = 5; // array size
    int *dArray = new int[size];


    return 0;
}
#包括
使用名称空间std;
int main(){
//标准阵列
int数组[5];

cout无法计算动态数组的大小,因此需要明确提供数组的大小

#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 size = 5; // array size
    int *dArray = new int[size];


    return 0;
}
#包括
使用名称空间std;
int main(){
//标准阵列
int数组[5];

cout它不可能与
sizeof
一起工作,因为
sizeof
是一个编译时运算符,但您需要一个运行时值。
sizeof(dArray)
只是
sizeof(int*)
的语法糖,而
sizeof(*dArray)
只是
sizeof(int)的语法糖
。两者都是编译时常量


sizeof(array)
工作的原因是
5
array
编译时类型(
int[5]
)的一部分。

它不可能与
sizeof
一起工作,因为
sizeof
是一个编译时运算符,但您需要一个运行时值。
sizeof(dArray)
只是
sizeof(int*)
的语法糖,而
sizeof(*dArray)
只是
sizeof(int)
的语法糖。两者都是编译时常量


sizeof(array)
工作的原因是
5
array
编译时类型(
int[5]
)的一部分。

它不可能与
sizeof
一起工作,因为
sizeof
是一个编译时运算符,但您需要一个运行时值。
sizeof(dArray)
只是
sizeof(int*)
的语法糖,而
sizeof(*dArray)
只是
sizeof(int)
的语法糖。两者都是编译时常量


sizeof(array)
工作的原因是
5
array
编译时类型(
int[5]
)的一部分。

它不可能与
sizeof
一起工作,因为
sizeof
是一个编译时运算符,但您需要一个运行时值。
sizeof(dArray)
只是
sizeof(int*)
的语法糖,而
sizeof(*dArray)
只是
sizeof(int)
的语法糖。两者都是编译时常量


sizeof(array)
之所以有效,是因为
5
array
编译时类型(
int[5]
)的一部分。

[这里有一个类似的问题。或者你可以使用std::vector。(::[1][1]:[这里有一个类似的问题。或者你可以使用std::vector。(:[1][1]:[这里有一个类似的问题。或者你可以只使用std::vector。(:][1][1]:[这里有一个类似的问题。或者你可以只使用std::vector。(:][1][1]: