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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:45:23+00:00 2026-05-26T20:45:23+00:00

I have std::ostringstream out; out << name << len << data; std::string const value

  • 0

I have

       std::ostringstream out;
       out << name << len << data;
       std::string const value = out.str();

I want to copy this const value to a char*

        char* buff; 
        size_t length = strlen(value); 
        buff = new char[(strlen(value))+ 1]; 
        memcpy(nm, value , length + 1); 

but it is still giving me error? Can you please tell me what I am doing wrong? Thank you.

  • 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-26T20:45:23+00:00Added an answer on May 26, 2026 at 8:45 pm

    Use value.size() instead of strlen(value).

     char* buff; 
     size_t length = value.size();
     buff = new char[length+1]; 
     memcpy(buff, value.c_str() , length + 1); 
           //^^^^  ^^^^^^^^^^^ also note this
    

    Or you can copy in this as well:

     std::string copy = value;
     const char * buff = copy.c_str();
    

    But note that buff will exist as long as the variable copy exists. In other words, the lifetime of buff is tied with the lifetime of the variable copy, and you don’t need to write delete buff in this case.

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

Sidebar

Related Questions

I am converting double values to string like this: std::string conv(double x) { char
I have byte to binary string function, std::string byte_to_binary(unsigned char byte) { int x
I have a std::vector<int> and I want serialize it. For this purpose I am
I have a simple problem with a conversion: std::string str = 0xC0A80A02 and I
let's say I have std::map< std::string, std::string > m_someMap as a private member variable
When using CDT I would like to have std::string show up in the 'variable'
I have several std::vector , all of the same length. I want to sort
I have a std::multimap where key is a custom class. Something like this: Class
I have a std::vector<int> , and I want to delete the n th element.
I have a std::vector. I want to create iterators representing a slice of that

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.