我一直在测试汽车的关键字,发现了奇怪的事情对我来说。每个字母需要1个字节(char类型),和自动变量的使用自动符大小为4个字节不管什么(我没有测试很长的字符串)。怎样才可以解释呢?
char carray[] = "Some test output";
auto variable = "Some test output";
cout<<"carray: "<<sizeof(carray)<<endl;
cout<<"auto: "<<sizeof(variable);
因为数组到指针的衰减,variable
成为const char*
(用A4尺寸,这对我来说有点令人惊讶 - ?什么是你的平台)。
如果你希望你的variable
仍然是一个字符数组,你可以使用decltype(auto)
,像
decltype(auto) variable = "Some test output"; // sizeof(variable) is 17
auto variable = "Some test output";
声明一个指针到字符。大小将是相同的机器字(4个字节的32位机器上,8个字节的64位机器上通常)的大小。