views:

55

answers:

1

The struct looks like this:

template <class Node_entry>
Node<Node_entry>::Node(Node_entry item, Node *add_on)
{
    entry = item;
    next = add_on;
}

And the *new_rear pointer does not get initialized, but &item is filled with user input.

   Error_code Extended_queue::append(const Queue_entry &item) {
        Node<Queue_entry> *new_rear = new Node<Queue_entry>(item);
        if(new_rear = 0)
            return overflow;
        if(rear = 0){
            front = new_rear;
            rear = new_rear;
        }
        else {
            rear->next = new_rear;
            rear = new_rear;
        }
        return success;
    }

In the locals in VS2010 this and new_rear are both (!) in the next and the entry, the item is good. What am I doing to get this "Access violation writing location 0x00000010." ?

+7  A: 

I assume you meant to say if (new_rear == 0), not if (new_rear = 0)? Your compiler should have given you a warning.

EDIT: In case you are wondering why it crashes - well, you're assigning 0 to the pointers, which also makes the conditions evaluate to zero, so you end up in the else block with "rear" just freshly assigned to 0, so "rear->next" ends up writing to 0x10.

Btw, this is why I always compile with the highest warning level, and warnings treated as errors. Warnings are your friends.

EboMike
As long as you don't have to use badly written libs spewing out warnings...
dutt
Wow... I didn't even notice. How do I turn on the warnings in VC2010? I usually use DEVCPP.
Knownasilya
Ugh yeah. Depending on your setup, you could compile them separately, and on some compilers you could temporarily turn off the warnings when you include the headers via pragmas. But sloppily written libraries are the devil.
EboMike
In the properties of your project. There's a setting for the warning level (1-4), and an option to treat warnings as errors.
EboMike