B
bArT
Hi!
I have a problem with such situation. I have a class like below and have
some pointers (ptr1 and ptr2). I dynamically allocate memory in constructor
and I free in destructor. For one pointer there is some error when
dustroctor delete objects. Why? What should I have to change?
Please help.
bart
class A
{
char *ptr1;
char *ptr2;
A(char *string1, char *string2)
{
if (string1 != NULL)
{
ptr1 = new char[strlen(string1)+1];
strcpy(ptr1, string1);
}
if (string2 != NULL)
{
ptr2 = new char[strlen(string2)+1];
strcpy(ptr2, string2);
}
}
virtual ~A()
{
if (ptr1 != NULL)
delete [] this->ptr1;
if (ptr2 != NULL)
delete [] this->ptr2;
}
// other methods. I use there ptr1 and ptr2
}
main()
{
A MyClass("text1", "text2")
return 0;
}
I have a problem with such situation. I have a class like below and have
some pointers (ptr1 and ptr2). I dynamically allocate memory in constructor
and I free in destructor. For one pointer there is some error when
dustroctor delete objects. Why? What should I have to change?
Please help.
bart
class A
{
char *ptr1;
char *ptr2;
A(char *string1, char *string2)
{
if (string1 != NULL)
{
ptr1 = new char[strlen(string1)+1];
strcpy(ptr1, string1);
}
if (string2 != NULL)
{
ptr2 = new char[strlen(string2)+1];
strcpy(ptr2, string2);
}
}
virtual ~A()
{
if (ptr1 != NULL)
delete [] this->ptr1;
if (ptr2 != NULL)
delete [] this->ptr2;
}
// other methods. I use there ptr1 and ptr2
}
main()
{
A MyClass("text1", "text2")
return 0;
}