计算中文在内存占用的字节数
#include<iostream>
using namespace std;
void main()
{
// 功能: 计算 中文在内存占用的字节数
char name[] = "成都";
char node_name[] = "成都移动04节点";
cout << "int占用:" << sizeof(int) << endl;
cout << "成都:" << sizeof(name) << endl;
cout << "成都:" << strlen(name) << endl;
cout << "成都移动04节点占用:" << sizeof(node_name) << endl;
cout << "成都移动04节点占用:" << strlen(node_name) << endl;
}
注:
- sizeof() 函数不统计字符串结束标识'\0'。
- strlen() 函数统计时包含字符串结束标识'\0'。
结果:
中文具体在内存中占用多少字节取决于你的电脑是64位还是32位。你可以执行上面的代码查看一下,当前大部分电脑都是64位的。