Question:
Can I deallocate pointer variables with a destructor in c++?
anonymous
2011-04-10 17:49:56 UTC
class ResistorClass
{
protected:
double *m_dptrRes;
char *m_sptrResName;
public:
static int m_istResCounter;
void DisplayResistor();
void EnterResistance();
ResistorClass();
ResistorClass(char[], double, double);
~ResistorClass();
};

/////////////////////The two constructors create dynamic arrays

ResistorClass::ResistorClass()
{
string name;
cout << "Enter the resistor name\n";
getline(cin,name);
int length = (int)name.length();
m_sptrResName = new char[length +1];
strcpy_s(m_sptrResName, (length + 1), name.c_str());
m_dptrRes = new double[4];
m_dptrRes[0] = 1000.0;
m_dptrRes[1] = .1;
m_dptrRes[2] = m_dptrRes[0] + (m_dptrRes[0] * m_dptrRes[1]);
m_dptrRes[3] = m_dptrRes[0] - (m_dptrRes[0] * m_dptrRes[1]);
m_istResCounter++;
cout << "\nDefault Constructor Called\n";

}

ResistorClass::ResistorClass(char name[], double nominal, double tolerance)
{
int length = sizeof(name) / sizeof(char);
m_sptrResName = new char[length];
m_sptrResName = name;
m_dptrRes = new double[4];
m_dptrRes[0] = nominal;
m_dptrRes[1] = tolerance;
m_dptrRes[2] = m_dptrRes[0] + (m_dptrRes[0] * m_dptrRes[1]);
m_dptrRes[3] = m_dptrRes[0] - (m_dptrRes[0] * m_dptrRes[1]);
m_istResCounter++;
cout << "\nParameterized Constructor Called\n";

}
/////////////////////and I want to delete those with the constructor

ResistorClass::~ResistorClass()
{
cout << "\nDestructor Called" << endl;
delete []m_sptrResName;
delete []m_dptrRes;
m_istResCounter--;
cout << "Number of DateClass objects instantiated " << m_istResCounter << "\n\n";
}
////////////////The program compiles fine but when I run it it says "Debug Assertion Failed" and the program won't complete?
Three answers:
The Phlebob
2011-04-10 17:59:44 UTC
Does it say why the assertion triggered? If it wasn't an ASSERT you put in, then it was a built-in one. Those often mean some C++ runtime function was called with a null pointer or an illegal pointer (dynamic memory not pointing into the heap, for instance, or a bad header structure). Test the pointer just before deleting it to see what it looks like.



This is the reason for assertions!
Ratchetr
2011-04-10 18:23:54 UTC
Not only can you, but you MUST.

If your class allocates memory, then the D'Tor needs to free it.



What is the test case that is failing?



These work for me:

ResistorClass r;

ResistorClass r2("Foo",1.2,0.01);



But this will fail (when main exits):

ResistorClass r3(r); // Will use the default copy constructor.



So will this:

ResistorClass r4 = r; // Will use default operator ==



The failures are Debug Assertion Failed, and they happen inside the destructor for r3 and r4.



I suspect you are doing something like that. You can't use the default copy constructor or operator =='s that the compiler generates when you have allocated memory in your class. The defaults will do a shallow copy. That means that m_sptrResName and m_dptrRes in r3 and r4 will end up pointing to the same memory that r points to. But when r's D'Tor is called, that memory has now been freed. When the D'Tor for r3 or r4 runs, you are trying to free memory that has already been freed. That's WILL trigger an assertion in a debug build (thankfully).



If that isn't what is going on, try to post the actual test case that fails. Boil it down to as few lines as you can.
santiesteban
2016-11-19 13:45:24 UTC
maximum possibly it provides you with a character representation from the handle of the int. a character is many times one byte long, whilst an int is 4 bytes, so as maximum ints could be low numbers, the 1st bit could be all zeros, so i could think of it may then be a pointer to the NULL character(in maximum situations).


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...