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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:07:42+00:00 2026-05-10T19:07:42+00:00

I have a char* p , which points to a \0 -terminated string. How

  • 0

I have a char* p, which points to a \0-terminated string. How do I create a C++ string from it in an exception-safe way?

Here is an unsafe version:

string foo() {   char *p = get_string();    string str( p );   free( p );   return str; } 

An obvious solution would be to try-catch – any easier ways?

  • 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. 2026-05-10T19:07:43+00:00Added an answer on May 10, 2026 at 7:07 pm

    You can use shared_ptr from C++11 or Boost:

    string foo() {     shared_ptr<char> p(get_string(), &free);     string str(p.get());     return str; } 

    This uses a very specific feature of shared_ptr not available in auto_ptr or anything else, namely the ability to specify a custom deleter; in this case, I’m using free as the deleter.

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

Sidebar

Ask A Question

Stats

  • Questions 61k
  • Answers 61k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Google 'EJB video'... a few to get you started EJB… May 11, 2026 at 9:45 am
  • added an answer Remote debugging is rather straightforward: on the target platform, launch… May 11, 2026 at 9:45 am
  • added an answer Sorry spoke too soon... As mentioned in another topic, this… May 11, 2026 at 9:45 am

Related Questions

I have a char* p , which points to a \0 -terminated string. How
If I have: signed char * p; and I do a comparison: if (
I have a char array in a C application that I have to split
I have a char array buffer that I am using to store characters that
I have a char* buffer and I am interested in looking at the first
I have a function that accepts a char* as one of its parameters. I
I have a big lump of binary data in a char[] array which I
Let's say I have a char* str = 0123456789 and I want to cut
I have a string(char*), and i need to find its underlying datatype such as
I have a string (char) and I want to extract numbers out of it.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.