为什么是sizeof(string)== 32? [英] Why is sizeof(string) == 32?
本文介绍了为什么是sizeof(string)== 32?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在字符串结构中导致sizeof()为32的开销是多少?
What is the overhead in the string structure that causes sizeof() to be 32 ?
推荐答案
c> std :: string 实现将非常小的字符串直接保存在静态大小为 char
的堆栈中,而不是使用动态堆存储。这允许避免大量小字符串对象的堆分配,并改进引用的局部性。
Some std::string
implementations save very small strings directly on the stack in a statically sized char
array instead of using dynamic heap storage. This allows to avoid heap allocations for lots of small string objects and improves locality of reference.
此外,还会有一个 std :: size_t
成员保存字符串大小和一个(可能未使用,见上文)指向堆存储的指针。
Furthermore, there will be a std::size_t
member to save the strings size and a (potentially unused, see above) pointer to the heap storage.
这篇关于为什么是sizeof(string)== 32?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文