0

I am a first grade student and this is my first time asking in stackoverflow.Recently my professor sent me a piece of code asking me to find out what was wrong with it.

#include <iostream>
#include <cstring>

using namespace std;

class A
{
public:
 A(int c);
 ~A();
 int code;
 char *name;
};

A::A(int c)
{
 code = c;
 name = new char[50];
 if(name == 0)
  {
   cout << "Unavailable memory" << endl;
   exit(1);
  }
}

A::~A()
{
 cout << "Delete pointer to " << name << endl;
 delete [] name;
}

int main(void)
{
 A a1(100), a2(200);
 strcpy(a1.name, "Mike");
 a2 = a1;
 strcpy(a2.name, "Peter");
 cout << a1.name << endl;
 return 0;
} 

As far as i know,the program should crash after the destructor of object a2 is called because the destructor of object a1 will try to delete the same already deleted memory.After this line:

a2 = a1;

the char* of object a2 points at the same address that the pointer of object a1 points,thus creating a mess upon calling the destructor.What really doesn't make sense to me is the fact that the program runs perfectly fine right now without any crashes,however yesterday the program crashed with the following output on console:

Delete pointer to Peter
Delete pointer to [insert strange characters here]
Process exited with return value 344233377

So im asking if someone could enlighten me regarding the strange behaviour of this program.BTW is it better to use operator overloading like this

#include <iostream>
#include <cstring>

using namespace std;

class A
{
 public:
 A(int c);
 ~A();
 A operator=(A a1);
 int code;
 char *name;
};

A::A(int c)
{
 code = c;
 name = new char[50];
 if(name == 0)
  {
   cout << "Unavailable memory" << endl;
   exit(1);
  }
}

A::~A()
{
 cout << "Delete pointer to " << name << endl;
 delete [] name;
}

A A::operator=(A a1)
{
 *name=*a1.name;
 code=a1.code;
 return *this;
}

int main()
{
 A a1(100), a2(200);
 strcpy(a1.name, "Mike");
 a2 = a1;
 strcpy(a2.name, "Peter");
 cout << a1.name << endl;
 return 0;
} 

or should i use something like a copy constructor?

John M.
  • 245
  • 1
  • 3
  • 13
  • You have violated the Rule of Three. – Kerrek SB Apr 28 '17 at 21:58
  • 1
    Reference page for the rule of three/five/zero: http://en.cppreference.com/w/cpp/language/rule_of_three – UnholySheep Apr 28 '17 at 21:59
  • You have violated every rule in every book concerning this topic. ` if(name == 0)` is unlikely to have the intended effect. – Captain Giraffe Apr 28 '17 at 21:59
  • So that means that i have to create a default constructor,a copy constructor and the destructor (which is already defined)? – John M. Apr 28 '17 at 22:00
  • why not just use a vector like a sane person would. Hmm, maybe even a string. – Captain Giraffe Apr 28 '17 at 22:02
  • 3
    _As far as i know,the program should crash after <...>_ No. The program exhibits undefined behavior, and crash is only one of the manifestations of it. – Algirdas Preidžius Apr 28 '17 at 22:06
  • @CaptainGiraffe You are right,however this piece of code was given to me by my professor.For example 'if(name==0)' was there just for "simplicity" and not for real error checking.Unfortunately i am not allowed to change the char* to a vector (and in fact i have never tried using a vector,though i have seen that they can protect from a lot of mistakes).However thanks for pointing it out! – John M. Apr 28 '17 at 22:08
  • @AlgirdasPreidžius Indeed, however all programs i have created until now and exhibit undefined behavior usually lead to a crash or an infinite loop. – John M. Apr 28 '17 at 22:11
  • @BenVoigt I think this vote was premature – Captain Giraffe Apr 28 '17 at 22:12
  • @UnholySheep Thanks for the link to this page,the content was really useful and helped me understand better my mistakes. – John M. Apr 28 '17 at 22:13
  • @CaptainGiraffe: You should explain why you think that. Anyway, is this better (duplicate of both "rule of 3 violation" and "why does undefined behavior sometimes appear to work" FAQ entries)? – Ben Voigt Apr 28 '17 at 22:17
  • 1
    **I am a first grade student**. In the US, first grade students are about 6 years old, and just learning to read and write, not how to program computers. Do you mean you're a first year university student? – Barmar Apr 28 '17 at 23:37
  • @JohnM. As you said it yourself: _usually_. It does not mean _always_. – Algirdas Preidžius Apr 28 '17 at 23:37
  • @Barmar Of course i am a first year university student.I didn't exaclty remember how it was called so i assumed that first grade student would suffice.Anyway thanks for pointing this out. – John M. Apr 29 '17 at 14:47

0 Answers0