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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:55:26+00:00 2026-06-01T13:55:26+00:00

I wanted to test how C++ behaves when the return value of a function

  • 0

I wanted to test how C++ behaves when the return value of a function is an object. I made this little example to watch how many bytes are allocated and determine whether the compiler makes a copy of object (like when object is passed as a parameter) or instead returns some kind of reference.

However, I couldn’t run this very simple program and I have no idea why. Error says: “Debug assertion failed! Expression: BLOCK_TYPE_IS_INVALID” in some dbgdel.cpp file. Project is a win32 console application. But I’m pretty sure that there is something wrong with this code.

class Ctest1
{
public:
   Ctest1(void);
   ~Ctest1(void);

   char* classSpace;
};

Ctest1::Ctest1(void)
{
   classSpace = new char[100];
}

Ctest1::~Ctest1(void)
{
   delete [] classSpace;
}

Ctest1 Function(Ctest1* cPtr){
   return *cPtr;    
}

int _tmain(int argc, _TCHAR* argv[])
{
   Ctest1* cPtr;

   cPtr=new Ctest1();


   for(int i=1;i<10;i++)
      *cPtr = Function(cPtr);


   delete cPtr;

   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-01T13:55:27+00:00Added an answer on June 1, 2026 at 1:55 pm

    You have violated the Rule of Three.

    Specifically, when you return an object, a copy is made, and then destroyed. So, you have a sequence of events like

    Ctest1::Ctest1(void);
    Ctest1::Ctest1(const Ctest1&);
    Ctest1::~Ctest1();
    Ctest1::~Ctest1();
    

    That is two objects are created: your original object construction, followed by the implicit copy constructor. Then both of those objects are deleted.

    Since both of those objects contain the same pointer, you end up calling delete twice on the same value. BOOM


    Extra Credit: When I investigate issues like “I wonder how the copies get made”, I put print statements in the interesting class methods, like this:

    #include <iostream>
    
    int serial_source = 0;
    class Ctest1
    {
    #define X(s) (std::cout << s << ": " << serial << "\n")
      const int serial;
    public:
       Ctest1(void) : serial(serial_source++) {
         X("Ctest1::Ctest1(void)");
       }
       ~Ctest1(void) {
        X("Ctest1::~Ctest1()");
       }
       Ctest1(const Ctest1& other) : serial(serial_source++) {
        X("Ctest1::Ctest1(const Ctest1&)");
        std::cout << " Copied from " << other.serial << "\n";
       }
       void operator=(const Ctest1& other) {
         X("operator=");
         std::cout << " Assigning from " << other.serial << "\n";
       }
    #undef X
    };
    
    Ctest1 Function(Ctest1* cPtr){
       return *cPtr;    
    }
    
    int main()
    {
       Ctest1* cPtr;
    
       cPtr=new Ctest1();
    
    
       for(int i=1;i<10;i++)
          *cPtr = Function(cPtr);
    
       delete cPtr;
    
       return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

simple example in Notepad++ using RegEx replace search for: anything replace with (wanted): \test
While reading this question I wanted to test the input in GCC to see
I have a console application that utilizes the BackgroundWorker object and wanted to test
I've made a simple app and I wanted to test pages for 404, 500
I have a simple function in jQuery and I wanted to test out the
Reading this question , I wanted to test if I could demonstrate the non-atomicity
Out of curiosity I wanted to test the number of ticks to compare a
I set up a new webapp Maven project and wanted to test it with
Hi I've been writing a chat client and wanted to test the Java Sound
I have built part of my app and wanted to test it out. All

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.