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 6990883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:25:01+00:00 2026-05-27T19:25:01+00:00

I am using the std::string type for my string manipulations. However, sometimes I need

  • 0

I am using the std::string type for my string manipulations.

However, sometimes I need to keep the raw char* pointer, even after the original std::string object is destroyed (yes, I know the char* pointer references the HEAP and must eventually be disposed of).

However, looks like there is no way to detach the raw pointer from the string or is it?

Maybe I should use another string implementation?

Thanks.

EDIT

Folks, please do not confuse detaching with copying. The essence of detaching is for the string object to relinquish its ownership on the underlying buffer. So, had the string had the detach method, its semantics would be something like this:

char *ptr = NULL;
{
  std::string s = "Hello world!";
  ptr = s.detach(); // May actually allocate memory, if the string is small enough to have been held inside the static buffer found in std::string.
  assert(s == NULL);
}
// at this point s is destroyed
// ptr continues to point to a valid HEAP memory with the "Hello world!" string in it.
...
delete ptr; // need to cleanup
  • 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-27T19:25:02+00:00Added an answer on May 27, 2026 at 7:25 pm

    No, it is not possible to detach the pointer returned by std::string::c_str().

    Solution: Make a read-only copy of the string, and ensure that that copy lives at least as long as you need the char* pointer. Then use c_str() on that copy, and it will be valid as long as you want.

    If that is not possible, then you won’t be able to release the char* either. And any attempt to wrap that pointer in a RAII construction, will only re-invent parts of std::string.

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

Sidebar

Related Questions

Using type std::string to accept a sentence, for practice (I haven't worked with strings
Up until now I have been using std::string in my C++ applications for embedded
I'm having a hard time using std::string::iterators in C++. This code compiles fine (still
Is std::string size() a O(1) operation? The implementation of STL I'm using is the
When using CDT I would like to have std::string show up in the 'variable'
I'm using the 'using' declaration in C++ to add std::string and std::vector to the
Using the STL C++ hash_map... class MyKeyObject { std::string str1; std::string str2; bool operator==(...)
I would like to compare 4 character string using wildcards. For example: std::string wildcards[]=
using namespace std; class A { public: A() {} ~A() {} map<int, string*>& getMap()
#include <iostream> #include <string> #include <fstream> using namespace std ; string strWord( int index

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.