Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SSO for strings stored in std::map

SSO enables short strings to be stored on stack. What if I have a std::map<std::string, std::string> (or any std container for that matter) which consists mainly of short strings (1 to 10 characters), and it grows to a big size (let’s say 200,000 pairs). With average length of 5, this will take up around 2 MB on characters alone, which is more than enough to cause stack overflow if SSO will work for all the strings.

So the question is: how likely is the stack overflow? How exactly SSO will work in that case (MinGW 6.0, C++14, Windows 10 x64 machine)?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your notion of "stored on the stack" is wrong. The std::map allocates its elements dynamically. Whether the std::string will dynamically allocate the characters is then not that relevant anymore.

What you store on the stack is sizeof(std::map<std::string,std::string>) and that is a compile time constant. It does not change with number of elements or length of the strings.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading