Gabriel Logan
Gabriel Logan

Reputation: 13

Segmentation Fault || Access Violation Writing location

I want to insert a Node in a doubly linked list. I'm passed in the position, the new coefficient of the Polynomial and its power. I have no compile errors, but I get a Segmentation fault in linux (g++) and an Access Violation Writing Location when I run it with Visual Studio.

Unhandled exception at 0x00bd20ba in Program.exe: 0xC0000005: Access violation writing location 0xcdcdcdd9.

void Polynomial::insert( Term *pos, double newCoefficient, int power )
{
    Term *newTerm = new Term; // create a new node to insert

    // Link the new node to previous and next, given the position
    newTerm->prev = pos->prev;
    newTerm->next = pos;
    newTerm->prev->next = newTerm; // Here's where I'm getting the error
    newTerm->next->prev = newTerm;

    // change the coefficient and power
    newTerm->coefficient = newCoefficient;
    newTerm->power = power;
}

What am I doing wrong and How do I fix this?

Upvotes: 1

Views: 1147

Answers (5)

Avinash
Avinash

Reputation: 191

Segmentation fault generally happens when you try to dereference a NULL pointer.

It is considered a good practice to use NULL checks while dealing with pointers.In this case it seems that pos->prev is NULL which is causing the segmentation fault.

Upvotes: 0

Michael Koenig
Michael Koenig

Reputation: 364

I wonder why you implement a doubly linked list by yourself. You could define a struct or class which contains coefficient and power members and use it as the value type of std::list. This would give you lots of list operations (such as inserting and deleting elements) for free. As a bonus, std::list comes with proper iterators (instead of position pointers) to be used in standard algorithms.

Since the rest of your Polynomial class is missing (it is a class, not just a namespace, is it?) it is difficult to provide more concrete help.

Upvotes: 0

Sufian Latif
Sufian Latif

Reputation: 13356

Well, if pos is the first node, then pos->prev must be NULL. In that case, the statement newTerm->prev->next = newTerm; would crash, because there's no such thing as NULL->next!

You should check explicitly if pos is the first node in the list, and place newNode accordingly.

// Link the new node to previous and next, given the position
newTerm->prev = pos->prev;
newTerm->next = pos;
if(pos->prev) newTerm->prev->next = newTerm;
newTerm->next->prev = newTerm;

Upvotes: 1

Anirudh Vemula
Anirudh Vemula

Reputation: 5

Please check whether in any instance of executing the program whether pos can be the first node in the list. If it is, then it will cause a segmentation fault as you are accessing a member of the NULL pointer.

Always consider extreme cases when you are programming and make sure you have put required conditionals for them.

Upvotes: 0

Michael Shmalko
Michael Shmalko

Reputation: 710

pos->prev is likely to be NULL or uninitialized. You must validate your inputs before using them...

Upvotes: 1

Related Questions