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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:50:29+00:00 2026-06-05T21:50:29+00:00

Possible Duplicate: Why is the destructor not called for the returned object from the

  • 0

Possible Duplicate:
Why is the destructor not called for the returned object from the function?

I wrote some C++ code (below), compiled it with GCC 4.6 and it ran successfully. But I don’t know why the destructor of classA isn’t called when returning from createA().

Since ca is a local variable in createA() (i.e. on the stack), I think its destructor should be called when returning from the function. But in fact, the destructor is only called once when returning from the main function.

Moreover, returning a local instance always works fine in this test. I wonder if it’s safe to return a local instance on a stack frame when the frame has been popped out after returning.

This is my code:

#include <iostream>
#include <string.h>

class classA
{
public:
    classA() { len = 0; v = 0; }

    classA(int a)
    {
        len = a;
        v = new int[a];
        for (int i = 0; i < a; i++)
            v[i] = 2*i;
    }

    ~classA()
    {
        if (v)
            {
            memset(v, 0, len * sizeof(int));
            delete [] v;
        }
    }

    int *v;
    int len;
};

classA createA(int a)
{
    classA ca(a);
    return ca;
}

using namespace std;

int main()
{
    int a = 10;
    classA ca = createA(a);
    classA *pca = &ca;
    for (int i = 0; i < a; i++)
        cout << pca->v[i];
    cout << endl;
    return 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-05T21:50:32+00:00Added an answer on June 5, 2026 at 9:50 pm

    This is called return value optimization.

    In short, the compiler doesn’t have to return a copy of the object to optimize the code.

    For ca is a local variable, i.e. on the stack […]

    Not necessarily. ca can be created directly in the calling context to prevent the extra copy. Copy elision is the only optimization that a compiler is free to perform that can alter expected behavior.

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

Sidebar

Related Questions

Possible Duplicate: Why has the destructor been called only once? Given the code below,
Possible Duplicate: Is destructor called if SIGINT or SIGSTP issued? My code like this:
Possible Duplicate: Will exit() or an exception prevent an end-of-scope destructor from being called?
Possible Duplicate: When to use virtual destructors? When should your C++ object's destructor be
Possible Duplicate: class has virtual functions and accessible non-virtual destructor I got this code
Possible Duplicate: Yield In VB.NET In C#, when writing a function that returns an
Possible Duplicate: Finding the Variable Name passed to a Function in C# In C#,
Possible Duplicate: iOS - Detecting whether or not device support phone calls? I'm writing
Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a
Possible Duplicate: throwing exceptions out of a destructor In C++ we should never throw

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.