Witam serdecznie
Piszę mały zaliczeniowy program w c++. Mój problem polega na tym, że gdy używam w destruktorze polecenia delete *char, (linijka jest zaznaczona strzałką) program wypluwa mi dziwne znaczki i nie działa. Kiedy tę linijkę z kodem usunę, wszystko wydaje się w porządku, ale obawiam się o wyciek pamięci.
Program jest pisany w code::blocks pod linuxem, jeśli chodzi o kompilację w windowsie - jest błąd w 20 linijce z przypisaniem pamięci w klasie - z tym też w miarę możliwości prosiłbym o pomoc jak to rozwiązać.
Ponadto, jeśli są tu jakieś inne błędy, będę wdzięczny za podpowiedzi :)
#include <iostream>
#include <string.h>
using namespace std;
class MyClass
{
public:
//MyClass();
struct element
{
char* sign;
int number;
struct element *pnext;
};
element *head = new element; // ostrzezenie o niezgodnosci
MyClass () //constructor definition
{
head = NULL;
};
virtual ~MyClass () //destructor
{
element *tmp;
while (head) {
tmp = head;
delete head->sign;
head = head->pnext;
//delete tmp->sign; //<<-------------- blad w tym miejscu
delete tmp;
}
}
void insertElement(char*, int number); //insert element to the list with alphabetical order
void removeElement(char*); //remove element from the list with any given string
void print();
int & value (char *);
};
MyClass ob;
/*There is inefficiency in insertElement in the value function. It search the char from the beginning from the beginning.*/
int main()
{
// int na = 5;
ob.print();
int i=ob.value((char*) "tata");
cout << i << endl;
ob.value("tata")=3;
cout << ob.value((char*)"tata") << endl;
ob.value((char*)"mama")=18;
ob.value((char*)"babcia")=5;
ob.value((char*)"tata")=24;
ob.value((char*)"amam")=9;
ob.value((char*)"tata")=10;
ob.print();
ob.value("tesciowa")=3;
ob.value("tesciowa")=13;
i=ob.value("tesciowa");
cout << i <<endl;
ob.print();
ob.value("komputer")=7;
ob.print();
//delete ob.head;
return 0;
}
void MyClass::insertElement(char* szstr, int na)
{
element *temp=new element;
temp->sign = new char(strlen(szstr)+1);
//element *temp2=new element; //previous element
element *p = head;
element *temp2= head; //if the head is the first element, there will be situation that temp2 will be prev.
cout << "You want to add element with char ' " << szstr << " ' and number " << na << "." << endl;
temp->sign = szstr;
temp->number = na;
temp->pnext = NULL;
if (head == 0) // The first element of the list; if any does not exist
{
head=temp;
return;
}
else
{
while (p)
{
if (strcoll(temp->sign, p->sign) < 1)
{
if (p==head)
{
head=temp;
head->pnext=temp2;
break;
}
else
{
temp2->pnext=temp;
temp->pnext=p;
break;
}
}
else
{
if(p->pnext==NULL)
{
//temp->pnext=NULL;
p->pnext=temp;
break;
}
temp2=p;
p=p->pnext;
}
}
}
}
void MyClass::removeElement (char* pa)
{
element *temp = head;
element *prev = head;
while (temp!=NULL)
{
if (strcoll(pa, temp->sign) == 0)
if (temp==MyClass::head) //if this is the first element
{
MyClass::head=MyClass::head->pnext;
delete temp;
cout << "element has been removed." << endl;
return;
}
else //if it is not the first
{
prev->pnext = temp->pnext;
delete temp;
cout << "The element has been removed" << endl;
temp = prev->pnext;
return;
}
else
{
prev = temp;
temp = temp -> pnext;
}
};
cout << "There is nothing to remove!" << endl;
}
void MyClass::print()
{
//element *ptr= new element;
//ptr=head;
element *ptr(head);
short ncon(1);
if (ptr == NULL)
cout << "The list is empty." << endl;
else
{
while (ptr!=NULL)
{
cout << ncon << ". ch: " << ptr->sign << ", int: " << ptr->number << endl;
ncon++;
ptr=ptr->pnext;
}
}
//delete ptr;
}
int &MyClass::value (char* pa)
{
element *temp = head;
//element *prev = head;
while (temp!=NULL) //if there exist the string in the list
{
if (strcoll(pa, temp->sign) == 0)
{
return temp->number;
}
else
{
temp=temp->pnext;
}
}
int ndef(10); //default number to add with new element
insertElement(pa, ndef);
return value((char*)pa);
}