我正在尝试编译以下代码:
#include <iostream>
using namespace std;
void show1(string text1[]) {
cout << "Size of array text1 in show1: " << sizeof(text1) << endl;
}
int main() {
string text1[] = {"apple","melon","pineapple"};
cout << "Size of array text1: " << sizeof(text1) << endl;
cout << "Size of string in the compiler: " << sizeof(string) << endl;
show1(text1);
return 0;
}
并且输出如下所示:
Size of array text1: 96
Size of string in the compiler: 32
Size of array text1 in show1: 8
我不明白,为什么sizeof运算符在同一个数组上工作,却在两个不同的点给出两个不同的输出?请解释。
sizeof()
函数返回对象的编译时间大小。这意味着,如果您的类型从堆中分配了一个内存块,则sizeof()
不会考虑该内存。
第一种情况,即