I'm just a 4-months newbie in C++, and I have an assignment to do a small phonebook program for DOS or FreeDOS. This is what i got from days after searching:

This is CTelrecord class that would save every record in details

class CTelRecord
{
private:
    int  szTime;                             
    char szName[_MAX_NAME_LEN_];              
    char szNumber[_MAX_NUMBER_LEN_];          
public:
    CTelRecord();
    CTelRecord(char *name, char *number);
    void SetRecord(char *name, char *number);  
    void SetRecTime(int);                     
                                                   
    int Compare(CTelRecord *);                
    void Show();                             
    void ShowName();                         
    void ShowNumber();                       
    void ShowRecTime(short int);          
    nTime TimeParser();                        
    void ModifyNumber(char *number);  
    void ModifyName(char *name);            
    int LookChar(char key);                     
    int SearchMore(char key[_MAX_NAME_LEN_]);  
    char *GetName()
    {
        return szName;                          
    }
    char *GetNum()
    {
        return szNumber;                        
    }
    int &GetTime()
    {
        return szTime;                      
    }
};

And this is CNode class for constructing each node of the linked list.

class CNode
{
private:
    CTelRecord *pData;                 
    CNode *pNext;                    
public:
    CNode()                          
    {
 
        pData=new CTelRecord;
        if (pData==NULL)
        {
            cout<<"Memory error!"<<endl;
            exit(0);
        }
        pNext=NULL;
    }
    CNode(CNode &node)                  
    {
        //cout<<"copying \n";
        node.pData=new CTelRecord;
        node.pNext=new CNode;
        if (node.pData==NULL||node.pNext==NULL)
        {
            cout<<"Memory error!"<<endl;
            exit(0);
        }
        node.pData=pData;
        node.pNext=pNext;
    };  
    void InputData(CTelRecord *pdata)   
    {
        pData=pdata;
    } 

    void ShowNode()      
    {
        pData->Show();

    }
    CTelRecord *GetData()       
    {
        return pData;
    }
    CNode *GetNext()         
    {
        return pNext;
    }
    ~CNode()
    {
        //cout<<"Deconstructing..."<<endl;
       //what should i do here?
    }
    friend class CList;           
};

And finally this is CList class to handle controls for my linked list.

class CList
{
    CNode *pHead;      
    CNode *pCurrent;   
public:
    CList()             
    {
        pHead=0;
        pCurrent=0;
    }
    ~CList()          
    {
        DeleteList();
    }
    void LoadListFromFile(char *datafile); 
    void WriteListToFile(char *datafile);   
    void InsertNode(CNode *);              
    void InsertNewHead(CNode *);          
    void InsertToEnd(CNode *);    
    void MoveCurrentToEnd();             
    void DeleteNode(CNode *);               
    void SearchNode(char *, CNode *SearchResult[_MAX_SEARCH_RESULTS_], int &ResultCount);
    CNode *GetInsertPosition(CNode *);      
    CNode *LookUp(CTelRecord &);          
    CNode *GetCurrent()
    {
        return pCurrent;            
    }
    CNode *GetHead()
    {
        return pHead;                
    }
    //void Show();                        
    void DeleteList();                     
    friend class CPhoneBook;            
};
void CList::DeleteList()
{
    CNode *pTemp;
    pCurrent=pHead;
    if (pHead!=NULL)
    {
        while(pCurrent->pNext!=NULL)
        {
            pTemp=pCurrent;
            pCurrent=pCurrent->pNext;
            delete []pTemp;
        }
    }
}

Thing that confuses me most the time is how to get it worked. Now it's working fine, but i'm not sure about the deconstruction of CNode class and CList class.
If i leave my CNode deconstruction function in peace, it works fine, but if i add this line too ~CNode() function:
if (pData) delete [] pData;
it will display memory error.

Can anyone tell me how to solve the deconstruction functions of CNode and CList?

Thanks in advance.

Recommended Answers

All 2 Replies

Well, your deconstructors look pretty good and should work. Now, there's one thing though that might be what's causing your errors.

There are two types of delete:

int * pInt = new int; // A pointer to a single int on the heap
int * pIntArray = new int[5]; // A pointer to an array of ints on the heap

// When freeing your variables on the heap you use two different
// calls to delete depending on if you want to free a single object or
// an array of objects:

delete pInt; // Deletes a single object
delete [] pIntArray; // Frees the entire array

When calling delete on an object, you automatically make a call to that objects destructor. So when you call delete on your cList, its destructor is called. In there it iterates through all its nodes it seems, deleting them one in turn. Now, since you are not dealing with arrays here, I suggest you skip the array deletion and stick to single object deletion, don't you think? The same applies when every node clears its data. Use delete on its dataobject, but leave its next pointer be, since it's taken care of in cList. You could set its nextpointer to NULL, but I don't really know if it will make any difference.

I hope this helps to solve your problem, and if this is not the solution just let me know, and we'll take a closer look on your implementations!

Emil Olofsson

Okay, i got it. I've just fixed that, i misunderstood the call of "delete []" and "delete" then now i remove the [] and it works fine.

Thanks for the post Emil.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.