0

I have a function here that destroys an iterator acting on a sorted list.

void SortedListDestroyTheIterator (SortedListIteratorPtr iter) 
{
    Node pt = NULL;
    Node prev = NULL;
    SortedListIteratorPtr walk;

    walk = malloc(sizeof(struct SortedListIterator)+1);
    SortedListPtr li = iter->list;

    for(pt = li->start; pt!=NULL; pt = pt->next) //problem line of code
    {
        walk = pt->info;
        //delete
        if(walk==iter){
            if(prev==NULL){
                li->start = li->start->next;
            }
            else
            {
                prev->next = pt->next;
            }
        }
        prev = pt;
    }
    free(iter);
}

I have figured out that a segmentation fault occurs with this assignment statement: pt = li->start. Now this line of code is creating a node that points to the starting node of the list. If I type in li->start it works, but once I add the assignment pt=li->start I get a segmentation fault. I do not understand why or how to fix this.

2 Answers 2

1

Is Node a node or a pointer to node? If Node is a structure, then the code should be:

Node * pt = NULL;
Node * prev = NULL;

Although if this fix works, I don't understand why the compiler didn't produce an error or warning.

The example code doesn't show how Node, SortedListPtr, SortedListIterator, or SortedListIteratorPtr are defined.

I didn't check to see if there are other issues.

Sign up to request clarification or add additional context in comments.

Comments

0

The line pt = NULL seems the culprit.

Look in the for loop code:

for(pt = li->start; pt!=NULL; pt = pt->next ) //problem line of code

1 Comment

As long as li->start != NULL, the for loop should be ok.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.