#include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
double arr[10]={1,2,3,4,5};
//这里可以得到整个数组的字节数
cout << sizeof arr << endl;
double *p=new double[10];
//这里得不到整个数组的字节数,得到的是一个指针的字节数
cout << sizeof p << endl;
delete []p;
return 0;
}
为什么sizeof不行,因为sizeof是一个编译期就可以得出答案的。但是new是到运行期才知道你要分配多大内存。不过c99标准说sizeof也可以算运行期的大小了。但是目前还没有看到一个编译器支持。
#include
"
stdafx.h
"
#include
<
iostream
>
using
namespace
std;
int
main(
int
argc,
char
*
argv[])
{
double
arr[
10
]
=
{
1
,
2
,
3
,
4
,
5
};
//
这里可以得到整个数组的字节数
cout
<<
sizeof
arr
<<
endl;
double
*
p
=
new
double
[
10
];
//
这里得不到整个数组的字节数,得到的是一个指针的字节数
cout
<<
sizeof
p
<<
endl;
delete []p;
return
0
;
}