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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:55:56+00:00 2026-06-10T17:55:56+00:00

I learned that one of the motivation for copy constructor using is to avoid

  • 0

I learned that one of the motivation for copy constructor using is to avoid of the follow crash in the program –

#include <iostream>
using namespace std;

class Employee {
public:
    Employee(int ID,char const * name){...};
    ~Employee(){...};

    // Methods
    void Print();

private:
    // members
    int m_ID;
    char* m_name;
};

void MyFunc(Employee emp) {

    cout << "in MyFunc \n ";

}

int main() {
    Employee One(1,"Garen Torosian");

// calling copy constructor
    MyFunc(One);

    cout << "In the end of main \n";

// Here the program crashes!
    return 0;
}

as you can see the program should be crash before the return 0; , but when I run that program it works fine and terminated ok , why ?

Edit : In this case the program indeed crash –

// Employee.h
#include <iostream>
using namespace std;

class Employee {
public:
   Employee(int ID, 
                   const char* name);
  ~Employee();

  // Methods
  void Print();

private:
  // members
  int   m_ID;
  char* m_name;
};

// Employee.cpp
#include "Employee.h“

Employee::Employee(int iID, const char *name){ // Ctor
  cout << "Constructor called" << endl;
  m_ID = iID;
  m_name = new char [strlen(name) +1];
  strcpy(m_name, name);
  Print();
}
void Employee::Print() { // Print
cout << "ID: " << m_ID << ",Name:” << m_name
<< " ,Address(pointer):0x" << hex << (int) m_name<< endl;
}
Employee::~Employee() { // Dtor
  cout << "Destructor called"<<endl;
  Print();
  delete [] m_name;
  m_name=NULL;
}

void MyFunc(Employee emp) {

  cout << "in MyFunc \n ";    

}

int main()
{
    Employee One(1,"Eli Rachamim");

// calling copy constructor
MyFunc(One);

cout<< "In the end of main \n“;

// Here the program crashes! 
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-10T17:55:58+00:00Added an answer on June 10, 2026 at 5:55 pm

    If you d-tor is like

    ~Employee(){ delete[] name; }
    

    and you alloc memory for your char* pointer, and you have no copy c-tor, than copy c-tor generated by compiler, that do memberwise-copy, will be called when you copy object. So, there will be double-free, that in most cases give you memory dump (in real calling destructor to already destructed object is UB and calling delete[] on already deleted object is also UB). But if you don’t work with memory – copy c-tor, generated by compiler works well.

    EDIT.

    So, your second example demonstrates call of d-tor to allready destructed object and also double-free.

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

Sidebar

Related Questions

One of the first things I learned in C++ was that #include <iostream> int
From what I learned, a MySQL MASTER server is one that can both read
I just learned (the hard way) that Java Component s can only have one
I've learned several languages, but now I want to choose one. The language that
Just today learned that one of my websites, TwitPeek.net, is not rendering properly in
From one of VBA tutorials I learned that variables contining numbers should be firstly
I just learned that an object can have only one ID, so I'm not
Since I use jQuery 1.3+ all except one timed test is using that. The
Today I learned that one of the .NET libraries I use, OpenTK, requires me
I am using DelphiIXE. I learned that GlobalMemoryStatus may return wrong results on 64

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.