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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:29:08+00:00 2026-05-30T07:29:08+00:00

When I have an object allocated not dinamically and I return it by value:

  • 0

When I have an object allocated not dinamically and I return it by value:

string get()
{
    string str("hello");
    return str;
}

There are chances to get memory leak? I make some examples:

int main(int argc, char** argv)
{
    string str=get(); // case 1
    get();              // case 2
    string* ptr=&get();    // case 3
}

In which cases there’s a memory leak?

  • 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-30T07:29:09+00:00Added an answer on May 30, 2026 at 7:29 am

    There are no memory leaks, but case 3 isn’t even vaguely valid.

    The get() function returns a copy of the string, possibly optimized, since std::string is a class and not a pointer or anything else. It works, from the user’s point of view, roughly the same as returning an int from a function: a copy is created and returned, and stays valid outside of the function scope so as to allow assignment or use. Without that behavior, return values in general wouldn’t be possible.

    It applies to PODs and classes very similarly (although the class needs a copy constructor and such). You should be able to do this for almost any std class, although the cost of making a copy will vary (it’s a bad idea to return a std::vector<BigClass>(1000), for example). Depending on settings, your compiler may be able to optimize the copy away through RVO.

    Case 1 uses that: the copy is assigned to str, no memory leak, no problem. Pretty simple.

    Case 2 discards the copy, but still, no problems.

    Case 3 will try to take the address of the temporary return value, which will quickly be destroyed by the compiler (due to being unused) and make ptr a hanging pointer. Any use of ptr after the temporary is gone will cause undefined behavior, probably an access violation. Where and when the temporary will be destroyed will depend on your compiler, settings, and other things you can’t rely on.

    The correct form is 1 (although 2 is also valid, just not very useful).

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

Sidebar

Related Questions

I have a memory leak with below code. If I use [sub release]; after
For the program below i get the following error- Potential leak of an object
I have a hashfactory that returns a dynamically allocated object. Item * Class::foo() {
I have object A which in turn has a property of type Object B
I have object A which contains multiple instances of object B, which in turn
I have two tables: object that has object_id column and avalues that have object_id
I think I read somewhere that some modules only have object oriented interfaces (
I have an object that is mapped to a cookie as a serialized base-64
I have an object graph serialized to xaml. A rough sample of what it
I have an object in a multi-threaded environment that maintains a collection of information,

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.