Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7632997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:45:59+00:00 2026-05-31T06:45:59+00:00

I always thought an std::string was implemented as an STL wrapper for a C

  • 0

I always thought an std::string was implemented as an STL wrapper for a C char array string. But looking closely at the design, I’ve noticed that it doesn’t give any hint or sign of being a wrapped up c-string. For all I know an std::string could be doing anything internally!

There is the c_str() method of course which I thought returned the internal char array, but how do I know if the method doesn’t create a new c char array from whatever data it stores inside and return it?

Seriously, how has std::string been implemented? Is it (as it appears to be) just a wrapper for a C char array, or is it something else? Or a mixture of the two? Or even can become both conditionally?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:46:01+00:00Added an answer on May 31, 2026 at 6:46 am

    For all I know an std::string could be doing anything internally!

    For all you know. The standard, of course, describes and demands certain semantics that rule anything out. It says the following on the basic_string template:

    §21.4 [basic.string] p1

    The class template basic_string describes objects that can store a sequence consisting of a varying number of arbitrary char-like objects with the first element of the sequence at position zero. Such a sequence is also called a “string” if the type of the char-like objects that it holds is clear from context. In the rest of this Clause, the type of the char-like objects held in a basic_string object is designated by charT.

    And a “char-like object” is defined by the following text:

    §21.1 [strings.general] p1

    This Clause describes components for manipulating sequences of any non-array POD (3.9) type. In this Clause such types are called char-like types , and objects of char-like types are called char-like objects or simply characters.

    This effectively means that you can stuff anything you want into basic_string, as long as it’s not an array and it is a POD (see this and this for infos on what PODs are). These char-like objects are then manipulated with the help of character traits, which define the specific behaviour of and relationship between them.


    […] but how do I know if the method doesn’t create a new c char array from whatever data it stores inside and return it?

    In C++03 exactly this was possible to do for the implementation, a known defect that has since been corrected in C++11:

    §2.4.1 [string.require] p5

    The char-like objects in a basic_string object shall be stored contiguously. That is, for any basic_string object s, the identity &*(s.begin() + n) == &*s.begin() + n shall hold for all values of n such that 0 <= n < s.size().

    See also these related questions:

    • Is string::c_str() allowed to allocate anything on the heap?
    • Is it legal to write to std::string?
    • Is it reasonable to use std::basic_string<t> as a contiguous buffer when targeting C++03?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've always thought it's the general wisdom that std::vector is implemented as an array,
I always thought that parentheses improved readability, but in my textbook there is a
I always thought that jinja was better because of template extending.. but them I've
I always thought that following code std::map<int, int> test; std::cout << test[0] << std::endl;
I always thought an SQL compiler would break but apparently nesing can nearly be
I always thought they were about the same thing but someone pointed me out
I always thought that if you didn't implement a heartbeat, there was no way
I always thought that when you dropped a control onto an .aspx page that
I always thought that internal class has access to all data in its external
I always thought that *&p = p = &*p in C. I tried this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.