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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:39:38+00:00 2026-05-26T05:39:38+00:00

I should preface this question by saying I think the answer is probably no,

  • 0

I should preface this question by saying I think the answer is probably no, but I’d like to see what other people think about the issue.

I spend most of my time writing C++ that interacts with the Win32 API which like most C style APIs wants to either:

  1. Take buffers which I’ve provided and operate on them.
  2. Or return pointers to buffers which I need to later free.

Both of these scenarios essentially mean that if you want to use std::string in your code you’ve got to accept the fact that you’re going to be doing a lot of string copying every time you construct a std::string from a temporary buffer.

What would be nice would be:

  1. To be able to allow C style APIs to safely directly mutate a std::string and pre-reserve its allocation and set its size in advance (to mitigate scenario 1)
  2. To be able to wrap a std::string around an existing char[] (to mitigate scenario 2)

Is there a nice way to do either of these, or should I just accept that there’s an inherent cost in using std::string with old school APIs? It looks like scenario 1 would be particularly tricky because std::string has a short string optimisation whereby its buffer could either be on the stack or the heap depending on its size.

  • 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-26T05:39:39+00:00Added an answer on May 26, 2026 at 5:39 am

    In C++11 you can simply pass a pointer to the first element of the string (&str[0]): its elements are guaranteed to be contiguous.

    Previously, you can use .data() or .c_str() but the string is not mutable through these.

    Otherwise, yes, you must perform a copy. But I wouldn’t worry about this too much until profiling indicates that it’s really an issue for you.

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

Sidebar

Related Questions

I should preface this by saying I'm working on a pocket PC app and
First off, let me preface this question by saying that my professor is firmly
BACKGROUND: I should preface this by saying I'm not trying to get someone to
Let me preface this by saying I am extremely new to git but have
Let me preface this question by saying that I am a .NET developer at
Preface : I'm honestly not sure if this should be on StackOverflow, SuperUser or
I'll preface this by saying this is my first Mac application, though I've been
Let me preface this by saying I'm a complete amateur when it comes to
I will preface this question with the disclaimer that I know there are many
Preface: This is not much about how to structure code within files. I have

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.