Enriquev
Enriquev

Reputation: 929

C++ pointer linked list

So I am new to c++ sorry if this is not to clear.

I have a class:

class Item
{
    int noItem;
    int qItem;
public:
    Item(int noItem, int qItem)
    {
            this->noItem = noItem;
            this->qItem = qItem;
    }

    int getNoItem()
    {
            return noItem;
    }

    int getQntItem()
    {
            return qItem;
    }
};

Then the following class:

class Element
{
public:
    Element()
    {
            data = NULL;
    }

    //to set and access data hold in node
    void setElement(Item *data)
    {
            this->data = data;
    }
    Item* getElement(void)
    {
            return(data);
    }

private:
    Item *data;
};

This one also:

class ListeChainee
{
public:

    ListeChainee()
    {
        courant = NULL;
    }
    void ajoutListe(Item *data)
    {
        Element *newData;

        //set data
        newData->setElement(data);

        //check if list is empty
        if( courant == NULL)
        {           
            //set current pointer
            courant = newData;
        }


    }

    //get data from element pointed at by current pointer
    Item* elementCourant(void)
    {
            if(courant != NULL)
            {
                return courant->getElement();
            }
            else
            {
                return NULL;
            }
    }

private:
    //data members
    Element *courant;           //pointer to current element in list

};

The code is missing some stuff for other things, but my problem is this:

int main(int argc, char* argv[])
{
    ListeChainee listeCH;
    Item i1(123,456);

    listeCH.ajoutListe(&i1);

    cout << listeCH.elementCourant()->getNoItem();

    system("pause");

    return 0;
}

I expect 123 to be outputted, but I see some other number. Not sure why. Thanks.

Upvotes: 0

Views: 75

Answers (2)

d3coy
d3coy

Reputation: 395

This method is writing to uninitialized memory:

void ajoutListe(Item *data)
{
    Element *new;

    //set data
    new->setElement(data); // Right here, "new" is an uninitialized pointer

    //check if list is empty
    if( courant == NULL)
    {           
        //set current pointer
        courant = new;
    }
}

I'm surprised this compiles (does it?). This code should also crash.

The strange number you're getting is surely some random part of memory. You may want to think more about memory management -- there are numerous problems here. When ajoutListe is called, why does the courant member only get set if it's NULL? Do we just leak the new Element? How do we actually traverse this list?

Upvotes: 1

ALittleDiff
ALittleDiff

Reputation: 1211

Your Element *newData doesn't have an instance of Element class, so it will crash when you try to access the instance pointed by newData.

Try to change Element *newData; to Element *newData = new Element;.

  • ps.: Don't forget to delete it when you don't need the instance any more.

Upvotes: 2

Related Questions