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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:11:39+00:00 2026-06-14T11:11:39+00:00

I am wondering if this would cause a memory leak or an undefined outcome

  • 0

I am wondering if this would cause a memory leak or an undefined outcome in C++?

string foo()
{
    char tempArray[30];
    strcpy(tempArray, "This is a test");
    return string(tempArray);
}

I know this is a bad thing in C but I haven’t found a definite answer for C++.

So everyone is saying no, but I am still confused as to when the memory is deallocated?

Lets say I have this method that calls the above method

void bar()
{
    string testString = foo();
}

At what point in the above code does the string object returned from foo() call its destructor? Is it immediately after getting copied into the object testString?

  • 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-14T11:11:40+00:00Added an answer on June 14, 2026 at 11:11 am

    What happens in your example is that the constructor with the signature

    string ( const char * s );

    is called. The constructor allocates new memory for a copy of the string and copies it to that new memory. The string object is then responsible for freeing its own memory when its destructor is called.

    When you make a copy of a string, the copy constructor also allocates memory and makes a copy.

    You should also take a look at the RAII pattern. This is how string‘s memory management works.

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

Sidebar

Related Questions

I was wondering what the parameters from this method would return. - (void) observeValueForKeyPath:(NSString
Wondering what the best / good way of doing this would be in jQuery.
I've been wondering what the best way to do this would be. I've got
Am wondering if there would be any security flaw in this approach. I am
I was wondering how I would be able to get this working? I've read
I was wondering if it would be possible to do this: $var = '$something
I am wondering if any one has experienced memory leak from using Handlebar.js compile
gcc 4.6.2 c89 I am just wondering what could cause more memory issues. For
I was just wondering this the other day. I am not exactly sure how
I've been wondering this for a while, and Google hasn't provided me with the

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.