473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

std::string implementation

Just out of curiosity, how is the std::string type implemented? In other words, what are the internals? Is it an immutable C string? Is it an over-allocated char[]? How does it work? Any insight would be appreciated.

(If you were wondering, I'm building an interpreter/runtime, and I need efficient strings for what I'm doing, and so I need to know whether I need to use my own implementation or not.)

Thanks in advance.
Mar 14 '09 #1
2 20075
weaknessforcats
9,208 Expert Mod 8TB
To quote P.J.Plaugher fo Dinkumware who provides STL templates for many major compilers such as Visual Studio.NET: "The templates are optimized for speed. If you think you can do it faster, then think three times."

That said, with the templates optimized for speed with footprint secondary, the string only has to support the methods defined by the ANS/ISO C++ Standard.

That said, the internal implementation can vary by whoever provides your STL templates.

One implementation looks like this:
Expand|Select|Wrap|Line Numbers
  1. class basic_string
  2. {
  3.     T str[19];
  4.     T* pstr;
  5.     //etc...
  6. };
Here memory allocation takes a lot of time so this implementation did a study and found that most strings are 19 elements or less so better to allocate that much right off when the string is created. Later, if more is needed, a heap allocation can be made and the 19 elements copied to it. From then on the 19 element array is not used.

Addtionally, if a string occupies, say 100 elements and is reduced to 50 elements, the allocation is not re-done to 50. Instead it stays at 100 in case you change your mind and need more later. Again, by not re-allocating the speed increases at the expense of more memory, speed increases.

In every analysis I have seen than compares the STL string to the C string library functions, the STL string is markedly faster.
Mar 14 '09 #2
Very in-depth analysis, thank you. I never knew strings were that fast. (Actually, my implementation would have been very similar, but now I know not to bother.)

Thanks again!
Mar 14 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Mathieu Malaterre | last post by:
Hello, This thread follow my previous one on the gcc mailing list. Basically I -still- have a problem in my code. I define in a header file: static const std::string foo = "bar"; Which not...
14
by: brad | last post by:
I've got a multithreaded application using std::string in Linux. Performance is not very good so I ran Quantify(tm) to look at what is happening. Most of the time my app was calling...
8
by: Jason Heyes | last post by:
If s is a std::string, does &s refer to the contiguous block of characters representing s?
15
by: roberts.noah | last post by:
Are there any decent benchmarks of the difference between using c strings and std::string out there? Google isn't being friendly about it. Obviously this would be dependant on different...
37
by: jortizclaver | last post by:
Hi, I'm about to develop a new framework for my corporative applications and my first decision point is what kind of strings to use: std::string or classical C char*. Performance in my system...
9
by: Fei Liu | last post by:
In Accellerated C++, the author recommends that in a header file one should not declare using std::string, using std::vector etc instead one should directly specify the namespace specifier in...
12
by: Vincent RICHOMME | last post by:
Hi, I am currently implementing some basic classes from .NET into modern C++. And I would like to know if someone would know a non mutable string class.
8
by: vidya.bhagwath | last post by:
Hello Experts, I am using std::string object as a member variable in one of the my class. The same class member function operates on the std::string object and it appends some string to that...
6
by: SteelSide | last post by:
Ive searched and searched,but havent been able to get it to work. ----------------- #include <iostream> using namespace std; std::string tempHostNameStr = "s1e2.hidden.thingy.org"; char...
25
by: Bala2508 | last post by:
Hi, I have a C++ application that extensively uses std::string and std::ostringstream in somewhat similar manner as below std::string msgHeader; msgHeader = "<"; msgHeader += a; msgHeader...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.