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

  • Home
  • SEARCH
  • 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 7049597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:02:17+00:00 2026-05-28T03:02:17+00:00

My code converts C++ strings to C strings somewhat often, and I am wondering

  • 0

My code converts C++ strings to C strings somewhat often, and I am wondering if the original string is allocated on the stack. Will the C string be allocated on the stack as well? For instance:

string s = "Hello, World!";
char* s2 = s.c_str();

Will s2 be allocated on the stack, or in the heap? In other words, will I need to delete s2?

Conversely, if I have this code:

string s = new string("Hello, mr. heap...");
char* s2 = s.c_str();

Will s2 now be on the heap, as its origin was on the heap?

To clarify, when I ask if s2 is on the heap, I know that the pointer is on the stack. I’m asking if what it points to will be on the heap or the stack.

  • 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-28T03:02:17+00:00Added an answer on May 28, 2026 at 3:02 am
    string s = "Hello world";
    char* s2 = s.c_str();
    

    Will s2 be allocated on the stack, or in the heap? In other words… Will I need to delete s2?

    No, don’t delete s2!

    s2 is on the stack if the above code is inside a function; if the code’s at global or namespace scope then s2 will be in some statically-allocated dynamically-initialised data segment. Either way, it is a pointer to a character (which in this case happens to be the first 'H' character in the null-terminated string_ representation of the text content of s). That text itself is wherever the s object felt like constructing that representation. Implementations are allowed to do that however they like, but the crucial implementation choice for std::string is whether it provides a "short-string optimisation" that allows very short strings to be embedded directly in the s object and whether "Hello world" is short enough to benefit from that optimisation:

    • if so, then s2 would point to memory inside s, which will be stack- or statically-allocated as explained for s2 above
    • otherwise, inside s there would be a pointer to dynamically allocated (free-store / heap) memory wherein the "Hello world\0" content whose address is returned by .c_str() would appear, and s2 would be a copy of that pointer value.

    Note that c_str() is const, so for your code to compile you need to change to const char* s2 = ....

    You must notdelete s2. The data to which s2 points is still owned and managed by the s object, will be invalidated by any call to non-const methods of s or by s going out of scope.

    string s = new string("Hello, mr. heap...");
    char* s2 = s.c_str();
    

    Will s2 now be on the heap, as its origin was on the heap?

    This code doesn’t compile, as s is not a pointer and a string doesn’t have a constructor like string(std::string*). You could change it to either:

    string* s = new string("Hello, mr. heap...");
    

    …or…

    string s = *new string("Hello, mr. heap...");
    

    The latter creates a memory leak and serves no useful purpose, so let’s assume the former. Then:

    char* s2 = s.c_str();
    

    …needs to become…

    const char* s2 = s->c_str();
    

    Will s2 now be on the heap, as its origin was on the heap?

    Yes. In all the scenarios, specifically if s itself is on the heap, then:

    • even if there’s a short string optimisation buffer inside s to which c_str() yields a pointer, it must be on the heap, otherwise
    • if s uses a pointer to further memory to store the text, that memory will also be allocated from the heap.

    But again, even knowing for sure that s2 points to heap-allocated memory, your code does not need to deallocate that memory – it will be done automatically when s is deleted:

    string* s = new string("Hello, mr. heap...");
    const char* s2 = s->c_str();
    // <...use s2 for something...>
    delete s;   // "destruct" s and deallocate the heap used for it...
    

    Of course, it’s usually better just to use string s("xyz"); unless you need a lifetime beyond the local scope, and a std::unique_ptr<std::string> or std::shared_ptr<std::string> otherwise.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code but it only converts strings to double. if the
I have this code that converts an array of date strings from a format
I've got some Python code that runs through a list of strings and converts
I need a function that converts an array of strings into a string that
I have placed together some code which converts strings into data, and then places
Somewhat my code looks like below: static int myfunc(const string& stringInput) { string word;
I have written this code to convert string in such format 0(532) 222 22
How do I convert a string into the corresponding code in PLT Scheme (which
We try to convert from string to Byte[] using the following Java code: String
I'm attempting to convert a string to a TStream. My code below gives me

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.