logicallayer
asked on
delete a char *ptr array inside a struct cause memory leak error
Hi,
I'm trying to implement a destructor but I'm getting an error of memory leakage of some kind
this isn't the actual code but it is how I'm having a problem with, mainly the part of delete the dynamic array of char inside the struct.
am I doing it right?
thanks,
I'm trying to implement a destructor but I'm getting an error of memory leakage of some kind
this isn't the actual code but it is how I'm having a problem with, mainly the part of delete the dynamic array of char inside the struct.
am I doing it right?
thanks,
class
{
public:
func();
private:
struct Node
{
char* text;
Node* link;
};
Node *head;
}
//////////////////////
class::func(const string& mytext)
{
head = new Node[sizeof(Node)];
head->text = new char[strlen(mytext.c_str())];
}
~class()
{
delete[] head->ptr; //here is the error happening
delete head;
}
It appears to me a link list kind of structure. It would certainly cause a memory leak. Before you delete the head, you have to delete all the element in the link list in a sequential fashion. You have to use your head and get the pointer to the next links. When all the links are deleted, then delete the head last of all. If it is a tree, you have to do the depth first search.
ASKER
sorry the destructor looks like this in the code bellow...
is this the right way to delete an array of char inside struct?
delete[] head->text; //here is the error happening
is this the right way to delete an array of char inside struct?
delete[] head->text; //here is the error happening
~class()
{
delete[] head->text; //here is the error happening
delete head->link; //here is error too
delete head;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks evilrix to bring to my attention that I'm creating an array of nodes...I missed that for some reason
I used a class as a demo example but actually I have a class name in my program...I have changed in the demo to avoid the confusion.
also I was stupidly creating a new instance of link instead of just using it as a pointer that is connecting two nodes...
please take a look at this code and let me know why my destructor is failing with
delete[] head->text;
I included more of my actual code that it might make more sense...let's say I called func few times in my main then I called the destructor...here where the error happens and when I comment out the
delete[] head->text;
then it works
thanks
I used a class as a demo example but actually I have a class name in my program...I have changed in the demo to avoid the confusion.
also I was stupidly creating a new instance of link instead of just using it as a pointer that is connecting two nodes...
please take a look at this code and let me know why my destructor is failing with
delete[] head->text;
I included more of my actual code that it might make more sense...let's say I called func few times in my main then I called the destructor...here where the error happens and when I comment out the
delete[] head->text;
then it works
thanks
class LinkedList
{
public:
LinkedList();
~LinkedList();
void func();
private:
struct Node
{
char* text;
Node* link;
};
Node *head;
Node *tail;
}
//////////////////////
void LinkedList::func(const string& mytext)
{
if(head == nullptr)
{
head = new Node;
head->text = new char[strlen(mytext.c_str())];
strcpy(head->text, mytext.c_str());
tail = head;
tail->link = nullptr;
}
else //if the first node been already created
{
Node *temp;
temp = tail;
tail = new Node;
tail->text = new char[strlen(mytext.c_str())];
strcpy(tail->text, mytext.c_str());
temp->link = tail;
tail->link = nullptr;
}
}
LinkedList::LinkedList()
{
head = nullptr;
tail = nullptr;
}
LinkedList::~LinkedList()
{
delete[] head->text; //here is the error happening
delete head;
}
ASKER
I solved the problem,it was with
head->text = new char[strlen(mytext.c_str() )];
tail->text = new char[strlen(mytext.c_str() )];
I changed them to
head->text = new char[strlen(mytext.c_str() ) + 1];
tail->text = new char[strlen(mytext.c_str() ) + 1];
and the memory leakage was fixed...
I will give the full credit to evilrix as his answer was very productive and it did hit the spot
have a good night everyone
head->text = new char[strlen(mytext.c_str()
tail->text = new char[strlen(mytext.c_str()
I changed them to
head->text = new char[strlen(mytext.c_str()
tail->text = new char[strlen(mytext.c_str()
and the memory leakage was fixed...
I will give the full credit to evilrix as his answer was very productive and it did hit the spot
have a good night everyone
Rather than posting code it would help to explain what you expect the code to specifically do.