Question

I try to write a bidirectional list. I must use overloaded operators([] and +=). [] - access to given node. += - add node to my list. I wrote these methods and it looks ok, but it doesn't work and I have no idea why.

here are core lines of my code:

//********************************************************
CLista::CNode* CLista::operator[](int i_index)
{
int i_help = 0;
CNode* c_result = &c_root;
while(i_help < i_index)
{
    c_result = c_result->getNext();
    i_help++;
}
return c_result;
}
//*********************************************************
void CLista::operator+=(void* pv_object)
{
if(i_numNodes == 0)
{
    c_root = CNode(pv_object);
}
else
{
    CNode c_new = CNode(pv_object);
    CNode* c_help = this->operator[](i_numNodes - 1);
    c_new.setPrevious(c_help);
    (*c_help).setNext(&c_new);
}   
i_numNodes++;
 }

 int _tmain(int argc, _TCHAR* argv[])
 {
CLista list = CLista();
string s1 = string("first");
void* wsk1 = &s1;
string s2 = string("second");
void* wsk2 = &s2; 
string s3 = string("third");
void* wsk3 = &s3; 

list += wsk1;
list += wsk2;
list += wsk3;


void* res1 = (*list[0]).getObject();
void* res2 = (*list[1]).getObject();
void* res3 = (*list[2]).getObject();

cout << "res1: " << res1 << endl;
cout << "res2: " << res2 << endl;
cout << "res3: " << res3 << endl;

cout << "wsk1:" << wsk1 << endl;
cout << "wsk2:" << wsk2 << endl;
cout << "wsk3:" << wsk3 << endl;
   }

and here is header:

class CLista
{
public:
    class CNode
    {
        public:
            CNode(void)
            {
                pc_next = NULL;
                pc_previous = NULL;
            }
            CNode(void* pv_object)
            {
                pc_next = NULL;
                pc_previous = NULL;
                this->pv_object = pv_object;
            }
            CNode* getNext(){return pc_next;};
            CNode* getPrevious(){return pc_previous;};
            void*  getObject(){return pv_object;};
            void setNext(CNode* pc_next);
            void setPrevious(CNode* pc_previous);
        private:
            CNode* pc_next;
            CNode* pc_previous;
            void*  pv_object; // czy to jest dobrze?
    };

    CNode c_root;
    int i_numNodes;

public:
    CLista(void);
    ~CLista(void);
    CNode* operator[](int index);
    void operator+=(void* object);

};

When I add third element to list and then check it, it is strange problem: addresses of res2 and res3 are the same.

Was it helpful?

Solution

In your operator += function, you create a local CNode called c_new that you link into your linked list. When the scope ends (which happens before the function returns), that local is destructed, leaving the list dangling (pointing at a no longer valid CNode, whose memory is about to be reused for some other local variable, such as the next c_new created on the next call to the function).

Using/accessing an object after it has gone out of scope and been destroyed is undefined behavior, so will generally crash or otherwise misbehave.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top