所以,如果我理解正确的话,电脑就会在32位的数据块(4字节),但在我的C++程序,我可以给一个变量的单字节的内存。这似乎是矛盾的我。
我想我的问题归结为:说我有一个程序,使用了大量的变量,所以空间问题。一个字节就足够了,我需要把我给他们节省空间为字符数据(1字节)。但是,如果计算机传送到最小的“块”是4个字节,这实际上节省空间吗?或将有效地使用相同的内存量为如果我变量整型(4字节,或一个完整的词)?
这是一个很微妙的问题,所以让我知道,如果我不清楚。
PS–感谢一个明确的,有组织的,写得很好的教程!我真的很喜欢到目前为止。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
int main()
{
using namespace std;
cout << "bool:\t\t" << sizeof(bool) << " bytes" << endl;
cout << "char:\t\t" << sizeof(char) << " bytes" << endl;
cout << "wchar_t:\t" << sizeof(wchar_t) << " bytes" << endl;
cout << "short:\t\t" << sizeof(short) << " bytes" << endl;
cout << "int:\t\t" << sizeof(int) << " bytes" << endl;
cout << "long:\t\t" << sizeof(long) << " bytes" << endl;
cout << "float:\t\t" << sizeof(float) << " bytes" << endl;
cout << "double:\t\t" << sizeof(double) << " bytes" << endl;
cout << "long double:\t" << sizeof(long double) << " bytes" << endl;
return 0;
}