Original Summary

I was reading this version of RE4B: https:&#x2F;&#x2F;github.com&#x2F;Cactus-proj&#x2F;RE-for-Beginners&#x2F;releases&#x2F;tag&#x2F;v1.0.0<p>The section about std::string (3.21.4) claims that the MSVC implementation of std::string looks like this:<p><pre><code> struct std_string { size_t unk1; union { char buf[16]; char ptr; } u; size_t size; &#x2F;&#x2F; Mysize size_t capacity; &#x2F;&#x2F; Myres }; </code></pre> However this structure is 24 bytes in 32 bit and 32 bytes in 64 bit. Meanwhile the current MSVC std::string is 28 bytes in 32 bit and 40 bytes in 64 bit.<p>I noticed that the book&#x27;s implementation is exactly 1 size_t short, so i tried adding a size_t to the top of the struct, and it worked perfectly, as in this dump function now outputs correctly:<p>#include &lt;string&gt; #include &lt;stdio.h&gt;<p>struct std_string { union { char buf[16]; char <i>ptr; } u;<p><pre><code> size_t size; &#x2F;&#x2F; Mysize size_t capacity; &#x2F;&#x2F; Myres</code></pre> };<p>void dump_std_string(std::string s) { struct std_string </i>p = (struct std_string )&amp;s; printf(&quot;[%s] size:%d capacity:%d\n&quot;, p-&gt;size &gt; 16 ? p-&gt;u.ptr : p-&gt;u.buf, p-&gt;size, p-&gt;capacity); }; int main() { printf(&quot;sizeof std string: %d\nours: %d\n\n&quot;, sizeof(std::string), sizeof(struct std_string));<p><pre><code> std::string s1 = &quot;a short string&quot;; std::string s2 = &quot;a string longer than 16 bytes&quot;; dump_std_string(s1); dump_std_string(s2);</code></pre> };<p>What has changed and what is that size_t that is now before the string buffer ? Is it some kind of reference counter ?


  • 情报分类:技术学习与提效
  • 分类依据:内容涉及技术、AI、软件工具或工程实践
  • 信息来源:Hacker News 新项目
  • 发布时间:2026/10/5 22:44:02