4

When I was looking at the way std::string is implemented in gcc I noticed that sizeof(std::string) is exactly equal to the size of pointer (4 bytes in x32 build, and 8 bytes for x64). As string should hold a pointer to string buffer and its length as a bare minimum, this made me think that std::string object in GCC is actually a pointer to some internal structure that holds this data.

As a consequence when new string is created one dynamic memory allocation should occur (even if the string is empty).

In addition to performance overhead this also cause memory overhead (that happens when we are allocating very small chunk of memory).

So I see only downsides of such design. What am I missing? What are the upsides and what is the reason for such implementation in the first place?

3
  • 2
    For myself, I usually find that it is a safe assumption that the compiler and standard library writers have given more thought to their designs than I have. I'd just assume there are good reasons, until I had a specific problem that might be caused by this. Now, you might be asking out of curiosity. In that case I would recommend that you do more research yourself -- the source code and its repository is right there :) Commented May 28, 2012 at 13:33
  • 3
    GCC std::string is implemented as copy-on-write smart pointer to the actual buffer. It's open source, so you can just read it. Commented May 28, 2012 at 13:35
  • Thanks, Jan, should have've found this myself. Please post as an answer, and I will accept Commented May 28, 2012 at 13:38

1 Answer 1

5

Read the long comment at the top of <bits/basic_string.h>, it explains what the pointer points to and where the string length (and reference count) are stored and why it's done that way.

However, C++11 doesn't allow a reference-counted Copy-On-Write std::string so the GCC implementation will have to change, but doing so would break the ABI so is being delayed until an ABI change is inevitable. We don't want to change the ABI, then have to change it again a few months later, then again. When it changes it should only change once to minimise the hassles for users.

Sign up to request clarification or add additional context in comments.

3 Comments

Any updates on this? Is the ABI change foreseeable? Can it be enabled conditionally?
The change might happen for GCC 4.9, and would take effect only when C++11 is used
@KerrekSB The C++11 ABI change happened in GCC 5.1: gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html (and there's a macro you can define to keep the old ABI, even through today's GCC 8.x).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.