0

I have created a linked list class VW with a function Build to add nodes to the list. However, when I run the code to build my linked list, the Build function throws an "access violation" error. The error is thrown by the call tmp->NextObj() in the Build function.

If the object of class VW is newly created and the Build is called for the first time, then the condition if(tmp==NULL) in the Build function should be true, and hence the program should not enter the else section during the first call to Build. What am I missing here? Could you please help.

   class VW
{
    World_Obj *head;
public:
    void Build(...);

};
 void VW::Build(...)
    {
        World_Obj *newObj;
        newObj = new World_Obj;
        newObj->SetData(...);
        newObj->SetNext(NULL);

        World_Obj *tmp;
        tmp = this->head;

        if (tmp == NULL)
            this->head = newObj;
        else
        {
            while (tmp->NextObj() != NULL)
            {
                tmp = tmp->NextObj();
            }
            tmp->SetNext(newObj);
        }
    }

int main()
{
    VW *g_w;    
    g_w = new VW;

          /* Reading arguments from input file*/

    g_w->Build(...);

}
7
  • Create a minimal example and don't use pictures that only contain text, they can't be indexed and searched. Commented Feb 17, 2016 at 7:19
  • 1
    Did you try to catch the crash in a debugger to help you locate where it happens? Commented Feb 17, 2016 at 7:22
  • Yes, the error occurs due to the call to tmp->NextObj() in the else section of the Build function. However, the first time Build is called, head should be null and the if condition should hold true. I am not sure what am I missing here. Commented Feb 17, 2016 at 7:58
  • Thanks Ulrich. I have updated the question based on your comments. Commented Feb 17, 2016 at 8:01
  • How do we know that head should be a null pointer? You don't show it being initialized anywhere. Please create a Minimal, Complete, and Verifiable Example and show us. Commented Feb 17, 2016 at 8:06

2 Answers 2

1

The issue was the missing constructor in the class VW,

VW(){ head=NULL;}

Thanks all!

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

Comments

0

Here is a big problem:

tmp = this->head;

...

while (tmp->NextObj() != NULL)
{
    tmp = tmp->NextObj();
}
tmp->SetNext(newObj);

...

delete tmp;

When the loop ends and after you do tmp->SetNext(newObj) then tmp will point to the second to last node in the list. Which you then promptly destroy by deleting it.

That means your list will now somewhere contain a node that no longer exist, and attempting to dereference it will lead to undefined behavior and most likely a crash.

The solution is simply to not delete tmp.

1 Comment

Thanks Joachim for the quick response. I removed the delete statement. However, I am still facing my earlier issue.

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.