Error when using Nodes in C++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Error when using Nodes in C++

  1. #1
    Join Date
    Jun 2017
    Posts
    2

    Error when using Nodes in C++

    Code:
    void LIST::insertEvent(EVENT *e)
    {
    	LLNODE* curr = front;
    	LLNODE* newn = new LLNODE(e);
    	LLNODE* old = NULL;
    	if (curr == NULL)
    	{
    		front = newn;
    	}
    	do {
    		old = curr;
    		curr = curr->next;   <------------------------------------------------------------------------------------the error spot
    	} while ((curr->ev->getprocessTime() <= newn->ev->getprocessTime()) && curr != NULL);
    	if (old == NULL)
    	{
    		newn->next = front;
    		front = newn;
    	}
    	old->next = newn;
    	newn->next = curr;
    }
    is my function, and

    Code:
    class LLNODE {
    	friend class LIST; //"LIST is my friend!"
    	
    	private:
    		LLNODE *next;
    		EVENT  *ev;
    		
    	public:
    		LLNODE(EVENT *e);
            ~LLNODE();
    };
    is the header function for it. At the line "curr = curr->next;" (in the do-while loop), there is a "Unhandled exception thrown: read access violation. curr was nullptr." error. Why is the exception being thrown there?
    Last edited by 2kaud; June 12th, 2017 at 01:47 AM. Reason: Added code tags

  2. #2
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    693

    Re: Error when using Nodes in C++

    Well, because simply your front data member is initialized as NULL. And then the current pointer is set as front (NULL).
    It'd would be interesting to see how the front member is being assigned.
    Popular opinion is the greatest lie in the world.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,636

    Re: Error when using Nodes in C++

    When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'.

    Cheers!
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,636

    Re: Error when using Nodes in C++

    Code:
    if (curr == NULL)
    	{
    		front = newn;
    	}
    	do {
    		old = curr;
    		curr = curr->next;
    As indicated by AvDav, when curr is null, you set front to newn but don't set curr. Then in the do loop you deference curr which is null so the exception is thrown.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)