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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:03:07+00:00 2026-05-11T14:03:07+00:00

I’m 99% certain the answer to this is a blinding no . Please validate

  • 0

I’m 99% certain the answer to this is a blinding no. Please validate my proposition that the following code will produce a memory leak.

Data &getData() {     Data *i = new Data();     return *i; }  void exampleFunc() {     Data d1 = getData();     Data d2;      /* d1 is not deallocated because it is on the heap, and d2 is      * because it is on the stack. */ } 

Please note that this is an oversimplified example, so clearly you wouldn’t actually use the above code… So no need to point this out thanks.

Update 1:

To add to this, what if I assign the pointer to a reference? In this case, I assume that the data is not copied…

Data &getData() {     Data *i = new Data();     return *i; }  void exampleFunc() {     // Does copying occur here?     Data &d1 = getData();      // Does this deallocate the memory assigned to the pointer?     delete &d; } 

Update 2:

I guess to answer my own question (in update 1) the following code proves that assigning a reference to a reference does not cause a copy…

#include <iostream> #include <string>  using namespace std;  class Data { public:     string mName;      Data(const string &name) : mName(name)     { cout << mName << ' default ctor' << endl; }      Data(const Data& other)     {         mName = other.mName + ' (copy)';         cout << mName << ' copy ctor' << endl;     }      ~Data()     { cout << mName << ' dtor' << endl; }      static Data &getData(const string &name)     {         Data *d = new Data(name);         return *d;     } };  int main() {     cout << 'd1...' << endl;     Data d1 = Data::getData('d1');      cout << 'd2...' << endl;     Data d2('d2');      cout << 'd3...' << endl;     Data &d3 = Data::getData('d3');      cout << 'return...'  << endl;     return 0; } 

Yields the following result…

d1... d1 default ctor d1 (copy) copy ctor d2... d2 default ctor d3... d3 default ctor return... d2 dtor d1 (copy) dtor 

Thanks to Eric Melski for a great answer (my code in update 2 is a modified copy of his exmaple code).

  • 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. 2026-05-11T14:03:07+00:00Added an answer on May 11, 2026 at 2:03 pm

    Actually both d1 and d2 will be deallocated, because they are both on the stack. What is not deallocated is the Data object you allocated in your getData() function. You can see this a little more clearly if you flesh out your Data class with instrumentation in the constructors and destructor. For example:

    class Data { public:     Data() { cout << "Data default ctor" << endl; }     Data(const Data& other) { cout << "Data copy ctor" << endl; }     ~Data() { cout << "Data dtor" << endl; }      static Data& getData()     {         Data *i = new Data();         return *i;     } }; 

    Note that I have explicitly declared the copy constructor for Data. In your example you are implicitly calling that constructor when you do Data d1 = getData();, and I suspect this is where your confusion is coming from. Now if I run this simple program:

    #include <iostream> using namespace std;  int main(int argc, char *argv[]) {     Data d1 = Data::getData();     Data d2;      return 0; } 

    The output is as follows:

    Data default ctor Data copy ctor Data default ctor Data dtor Data dtor 

    Line-by-line, here’s what you’re seeing:

    1. The default constructor is invoked when you allocate a new Data in getData().
    2. The copy constructor is invoked to create d1 from the dynamically allocated Data you just made.
    3. The default constructor is invoked to create d2.
    4. The destructor is invoked for d2 when it goes out of scope at the end of main().
    5. The destructor is invoked for d1 when it goes out of scope at the end of main().

    Note that there are three constructor calls, but only two destructor calls — indicating that you have leaked exactly one Data object.

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

Sidebar

Ask A Question

Stats

  • Questions 88k
  • Answers 88k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Per http://msdn.microsoft.com/en-us/library/f7yhsd2b(VS.80).aspx , "If you want to handle Windows notification… May 11, 2026 at 5:43 pm
  • Editorial Team
    Editorial Team added an answer If the problem is "multiply every element in each row… May 11, 2026 at 5:43 pm
  • Editorial Team
    Editorial Team added an answer This is the shortest superstring problem: find the shortest string… May 11, 2026 at 5:43 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.