Sap Labs Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
three problems
1) u have called destructor explicitly and will be also called at the end of prog
so trying to free memory twice.......
2) after calling destructor s->~A() u is assigned to S
3) copy constructor is not present.... default copu constructor do shallow copy
here deep copy is reqd
A s("Object s");
A t=s;
only above two lines in main will also give error.............
It will show core dump, because you have explicitly deallocate the memory of object "s" and in next statement u are assigning the object "s" to another object, so shallow copy can not takes place because s has no memory.
Another reason for core dump is one memory has been deallocated two times .
the only problem in this code is that it did not have a copy and assignment constructors and my code is here.....
#include <iostream>
#include <string.h>
#include<cstdio>
using namespace std;
class A
{
char *p;
public:
A(const char* str)
{
cout<<"parameterized constructor"<<endl;
p=new char[strlen(str)+1];
strcpy(p,str);
}
~A()
{
delete p;
}
A& operator =(const A &a)
{
cout<<"assignment constructor"<<endl;
p=new char[strlen(a.p)+1];
strcpy(p,a.p);
}
void print(){ cout<<"string= "<<p<<endl;}
A(const A &a){
cout<<"copy constructor"<<endl;
p=new char[strlen(a.p)+1];
strcpy(p,a.p);
}
};
int main()
{
A s("Object s");
s.print();
A t = s;
s.~A();
A u("Object u");
u=s;
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
class A
{
private:
char* p;
public:
A(const char* str)
{
if (str == NULL)
{
throw "Null pointer exception.";
}
p = new char[strlen(str) + 1];
strcpy(p, str);
cout << "Parameterized constructor: " << p << endl;
}
~A()
{
if(p != NULL)
{
cout << "Destructor: " << p << endl;
}
delete p;
p = NULL;
}
A& operator=(const A &a)
{
if (a.p == NULL)
{
throw "Null pointer exception.";
}
if (p != NULL)
{
delete p;
}
p = new char[strlen(a.p) + 1];
strcpy(p, a.p);
cout << "Assignment operator: " << p << endl;
return *this;
}
A(const A& a)
{
if (a.p == NULL)
{
throw "Null pointer exception.";
}
p = new char[strlen(a.p) + 1];
strcpy(p, a.p);
cout << "Copy constructor: " << p << endl;
}
};
int main()
{
try
{
A s("Object s");
A t = s;
s.~A();
A u("Object u");
u = s;
}
catch(const char* msg)
{
cout << msg << endl;
return -1;
}
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
class A
{
private:
char* p;
public:
A(const char* str)
{
if (str == NULL)
{
throw "Null pointer exception.";
}
p = new char[strlen(str) + 1];
strcpy(p, str);
cout << "Parameterized constructor: " << p << endl;
}
~A()
{
if(p != NULL)
{
cout << "Destructor: " << p << endl;
}
delete p;
p = NULL;
}
A& operator=(const A &a)
{
if (a.p == NULL)
{
throw "Null pointer exception.";
}
if (p != NULL)
{
delete p;
}
p = new char[strlen(a.p) + 1];
strcpy(p, a.p);
cout << "Assignment operator: " << p << endl;
return *this;
}
A(const A& a)
{
if (a.p == NULL)
{
throw "Null pointer exception.";
}
p = new char[strlen(a.p) + 1];
strcpy(p, a.p);
cout << "Copy constructor: " << p << endl;
}
};
int main()
{
try
{
A s("Object s");
A t = s;
s.~A();
A u("Object u");
u = s;
}
catch(const char* msg)
{
cout << msg << endl;
return -1;
}
return 0;
}
The problem is that in the line
- Luv June 29, 2012You have explicitly called the destructor for s object and then at the closing brace of the main function destructor is again been called i.e. what the C++ guarantees.
And if freed memory is again freed, it is undefined behaviour by the Standard.