views:

199

answers:

3

I've got a linked list class and for some reason when I increment an int in one object e.g linked1.size for some reason linked2.size also increments!

And ideas why this is? I didn't intentionally make it a static variable.

my code:

main()
{    
Vlist v1;  
v1.add(1,0);  
v1.add(2,0);  

Vlist v2;
}

Incrementing the size member variable occurs in the add() function like this:

(*this).size++;

The result should be the v1.size==2 and v2.size==0, but instead v2.size==2!

This problem has been driving me crazy for hours- any help would be really appreciated!

The add function is as follows:

int Vlist::quietAdd(Vertex new_vertex, int i_loc)
{
Vnode* temp= first_node;
Vnode* newNode= NULL;

//check for unique

if (find(new_vertex)!=999)
return 0;

//check for non-negative i value
if (i_loc<0)
{
    cout<<"Invalid index."<<endl;
    return 0;
}
//and exit here?

else
{
    temp = find(i_loc);

    newNode= new Vnode();

    if (size==0)
        first_node= newNode;

    //assigning to the new vnode the new Vertex value
    (*newNode).updateVertex(new_vertex.getInt());

    //the nxt pointer now points to the value it's replacing or NULL
    (*newNode).updateNextPoint(temp);

    if ((temp==NULL)&&size!=0)
    {
        //size-1 is used to get the pointer to the last value on the list
        (*newNode).updatePrevPoint(find(size-1));
        (*find((size-1))).updateNextPoint(newNode);
    }

    if (temp !=NULL)
    {
        //the new vnode's prev pointer now points to correct location
        (*newNode).updatePrevPoint((*temp).getPrevPoint());

        if ((*temp).getPrevPoint()!=NULL)
            /*the vnode that used to point to the existing vnode now
            points to new vnode*/
            (*((*temp).getPrevPoint())).updateNextPoint(newNode);

        //the old one's prev pointer points back to the new value
        (*temp).updatePrevPoint(newNode);
    }

    /*if we've just put a new vnode at the start then it should be
     pointed to by the "first vnode" pointer*/
    if (i_loc==0)
        first_node=newNode;

    (*this).size++;

}
    return 1;
}

//Vlist class definition
class Vlist
{
private:
    int size;
    Vnode* first_node;

public:
    //copy constructor
    Vlist(const Vlist& vl2):size(vl2.size), first_node(NULL)
    {
        for (int i=0;i<size;i++)
            quietAdd(vl2.read(i),i);
    }

    Vertex getNext();
    Vlist (): size(0)  {   first_node=0;    }
    ~Vlist (){};//make deep!
    bool empty();
    Vnode* find(int i_loc) const;
    int find(Vertex target)const;
    void add(Vertex new_vertex, int i_loc);
    int quietAdd(Vertex new_vertex, int i_loc);
    Vertex remove(int i_loc);

    Vertex read(int i_loc) const;
    Vnode* getFirstNode() {return first_node;}
    int getSize() const { return size;}

    void setSize(int newSize) { size = newSize;}

    char* print() const;
    void delete_List();
};

class Vnode
{
private:
    Vertex vertex;
    Vnode* prev_node;
    Vnode* nxt_node;

public:

    Vnode ()
         : prev_node(0), nxt_node(0)
    {
        vertex.update(0);
    }
    ~Vnode (){}; //destructor
    Vertex getVertex(){return vertex;}

    int getVertexInt() { return vertex.getInt();}
    Vertex getNext(){return (*nxt_node).getVertex();}
    Vnode* getNextPoint()    {   return nxt_node;    }
    Vnode* getPrevPoint()    {   return prev_node;    }
    void updateNextPoint(Vnode* newP) { nxt_node = newP;}
    void updatePrevPoint(Vnode* newP)    {prev_node= newP;}
    void updateVertex(Vertex vertexVal)  {vertex.update(vertexVal.getInt());}
};
+1  A: 

Without seeing more code, I can't be sure, but is it possible v1 and v2 are actually references to the same linked list object?

lc
They don't seem to be the same structure, because v2 remains empty, except for the size being incremented.
Dave
+2  A: 

Might it be that linked1 and linked2 somehow point to the same structure? You can try

printf("adress1 %p", &linked1)
printf("adress2 %p", &linked2)
printf("adress1/size %p", &linked1.size)
printf("adress2/size %p", &linked2.size)

For other members of Vlist repectively (&linked1.data?)

Edit: Now that the complete code is visible (and given that add_quiet(...) and add(...) do the same thing in principle) I don't think a "shared" size class field is the problem. Do use a debugger and track the adresses of your lists. This is rather strange, but I'm interested in the solution now more than ever

msiemeri
A: 

It seems like the debugger was telling me what the values were for v2, even before it was declared for some reason. (I'm using codeblocks).
In the end I just decided to declare these variables at the top of the page and this particular problem is fixed.
I was hoping to discover some design flaw that would explain a few other problems which are pretty difficult to describe without uploading my entire project onto this website. No such luck.
As to the rest of my program...sigh.

Anyway, Thanks for all your help.

Dave
What compiler did you use underlying to Code::Blocks? I guess, g++ was your choice?
msiemeri