0%

为什么底层 C/C++ 代码的 char[] 比 string 多很多?

  • https://www.zhihu.com/question/361487567/answer/939487994

网络通信用char[]比string要更常见。以下两个结构体一个用char[]一个用string:

1
2
3
4
5
6
7
8
9
10
11
12
struct PackageA {
int a;
int b;
int buff_len;
char buff[100];
};

struct PackageB {
int a;
int b;
string buff;
};

两个都可以达到要求,但是用起来PackageA其实更简单,因为PackageA是POD,整个结构体的数据是在一块内存中,可以用memset清零,可以用memcpy来拷贝,甚至可以将整个PackageA的内容通过socket发送到另一台机器上去(后台开发可以这样),省掉打包和解包的过程。而且对于多个PackageA,可以非常简单地使用mempool,因为内存连续。

但是对于PackageB就不行,string存储的内容和其它成员的内容在内存布局上不是在一起,不能整块拷贝和清零,需要为结构体定义构造函数和析构函数,由于多了string的构造函数和析构函数的调用,性能肯定比不上PackageA,而且对于PackageB要使用内存池也更麻烦。

评论区有的人说变长就不行了。变长这样定义:

1
2
3
4
5
6
struct PackageA {
int a;
int b;
int buff_len;
char buff[0];
};

这样你想要多长都可以。比如说你想要100G的长度,就这样用:

1
2
3
char* buff = new char[100G + sizeof(PackageA)];
PackageA* p = (PackageA*)buff;
p->buff_len = 100G;

不过我自己在服务器编程多年用的都是定长,因为无论如何变长,长度都必须指定一个上限,用上限值作为定长buffer的长度即可。

What are POD types in C++?

  • https://stackoverflow.com/questions/146452/what-are-pod-types-in-c

POD stands for Plain Old Data - that is, a class (whether defined with the keyword struct or the keyword class) without constructors, destructors and virtual members functions. Wikipedia's article on POD goes into a bit more detail and defines it as:

A Plain Old Data Structure in C++ is an aggregate class that contains only PODS as members, has no user-defined destructor, no user-defined copy assignment operator, and no nonstatic members of pointer-to-member type.

Greater detail can be found in this answer for C++98/03. C++11 changed the rules surrounding POD, relaxing them greatly, thus necessitating a follow-up answer here.