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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:12:30+00:00 2026-06-02T02:12:30+00:00

I borrowed this from another question about fragmentation, but I’m not bothered by that.

  • 0

I borrowed this from another question about fragmentation, but I’m not bothered by that. I’m more worried that I don’t understand the function at all. in terms of types and data lifetimes 🙁

The same data is represented by a std::vector, (a dynamic aray type with internal metadata), a pointer to the string data therein, (return parameter), and the declared return type, which is a std:string.

QUESTIONS:
How does the data get safely out of the function when the std::vector is going to be destroyed? Is it implicitly copied? Is the dynamic array of char in the vector ‘detached’ from the vector and returned as a std::string type so that no bulk copy is required? Sometimes, I think that C++ and the std library is trying to get me…

I’ve been using C++ for some while but stuff like this does my ‘ed in.

std::string TestFragmentation()
{
    std::vector<char> buffer(500);
    SomeCApiFunction( &buffer[0], buffer.size() ); // Sets buffer to null-terminated string data
    return &buffer[0];
}
  • 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-06-02T02:12:33+00:00Added an answer on June 2, 2026 at 2:12 am

    The data stored by a std::vector is guaranteed to be contiguous, so &buffer[0] gets you a raw pointer to the beginning of that data.1

    And std::string has a constructor which takes a const char *, which copies the data. That is being implicitly called in the return statement (the compiler is allowed to call at most one implicit conversion operation to avoid compile-time errors).

    In both cases (the vector and the string), the memory for the corresponding backing buffer is managed by the container class, so there is no possibility of a memory leak or similar (so long as your raw C function creates a valid null-terminated C-style string and doesn’t trample beyond the buffer bounds).


    1. Note, however, that there are no guarantees that it will stay in one place. As soon as you grow or shrink the vector, it’s likely that it will be copied elsewhere in memory, invalidating all raw pointers that were pointing at it. And if the vector itself is destructed, then of course the backing data is no longer valid.

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

Sidebar

Related Questions

This is borrowed from another question I made on the site, but would have
Title borrowed from this question , of which this one is not a duplicate.
Take the code below, adapted from this question : //Borrowed from another question because
#&q=car&category=Car%20Audio%2CAccessories&brand= I borrowed a this function from a previous question asked on SO: function
The following gist: https://gist.github.com/1876791 (borrowed from the demo in this question ) is a
This following code example is borrowed from MSDN here . I am not getting
I borrowed some code from a site, but I don't know how to get
I have borrowed code from this link PHP regex templating - find all occurrences
I am implementing a MyGestureDetector that extends a SimpleOnGestureListener. I borrowed the class from:
I want to chmod recursively in my build.xml and borrowed the following from this

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.