Thread: link list build: memory access denied

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    44

    link list build: memory access denied

    hi guys. I created a simple code to build linked list.
    following are the various files

    Header file
    Code:
    class ListBuild
    {
    private:
    	struct node {
    		         int d;
    			 node * next;  // next pointer
    	};
    	typedef node * list;  // list == node*
    	list head ,tail,prev;           // pointer to head and tail static member
        static int sz;              // current size of list,static member
    public:
    	ListBuild () ;                    // default constructor 
    	ListBuild (const ListBuild & Lst);   // copy constructor
    	~ListBuild() ;                   // default destructor 
    	int findlength()const;        // find size
    	void insert(int newitem);    // add node
    	void findpr()const;           // previous pointer that tail
    	void remove();  // remove node
            void print();      // print list
    
    };
    Implementation file
    Code:
    #include <iostream>
    #include "Link_list_build.h"
    using namespace std;
    
    int ListBuild::sz=0;
    ListBuild ::ListBuild()                    // default constructor 
    {
    
    head=0;                           //Null pointer
    tail=0;
    prev=0;
    }
    
    ListBuild ::ListBuild(const ListBuild& lst)   // copy constructor
    {
    
    }
    
    ListBuild ::~ListBuild()            // default destructor 
    {
    remove() ;               // call func to remove node memory
    }
    
    int ListBuild::findlength()const      // determine current size of list
    {
    	return sz;
    }
    
      void ListBuild ::insert(int newitem)   // add item at start or inbetween
    {
    	//int newlngth= findlength()+1;
    
    	
    	 // creates new node structure and returns pointer to it
    		 list newnode = new node ; 
    		 if( newnode ==NULL)
    			 cout<< " could not allocate memory";
    		 else
    		   {
    			 sz= findlength();
    			 newnode->d = newitem;
    			 newnode->next=NULL;
    			 if (sz==0) // allocate first node
    			   {
    				 head= newnode;
    				 tail=head;
    			   }
    			 else          // allocate rest nodes
    			  {
    				  tail->next = newnode;
    				  tail=newnode;
    			  }
    			 sz++;
    
    		 }  // end of if( newnode ==NULL)
      }
    
      void ListBuild::findpr()const
      {
           if (sz==0)
    	   cout<< "already empty list"<<endl;
    	   else if(sz==1)
    	     {   }  // do nothing
    	   else
    	   {
    	     list prev = head;
    	     for (int i=0;i<sz-1;i++)
    		    {
                            prev= prev->next;
    		    }
    	   }
      }
    void ListBuild ::remove()        // remove item
    {
    	        
    	   	         --sz;     
    		   	 if (sz==0)
    			 cout<< "already empty list"<<endl;
    			 else if (sz==1) // delete first node
    			    {
    				 delete tail;
                                      tail=NULL;
    			    }
    			 else          // delete rest nodes
    			    {
    			        list curr;
    				curr= prev->next; // now points to node be deleted
    				prev->next = curr->next;
    				curr=NULL;
    				delete curr;
    			    }
    }
    
    
    
    void ListBuild ::print()  
    {
    	  list temp=NULL;
    	   temp=head;  // dont want to change head pointer, so create another temp                              //pointer
    	   
    	   for (int i=0;i<sz;i++)
    	   {
    		   cout<< temp->d<<'\t';
    		   temp=temp->next;
    	   }
    }
    and the user file

    Code:
    #include <iostream>
    #include "Link_list_build.h"
    using namespace std;
    
    int main()
    {
      ListBuild a,b,c ;
      a.insert(10); // insert at start
      a.print();
      b.insert(20);  // insert at end and build list
      b.print();
      c.insert(30);  
      c.print();
    }
    .

    the code compiles and allocates first node. but, as i pass second node data, it reaces
    tail->next = newnode;
    within "insert" function and gives memory access violation.
    any ideas why? I do not see why it should.

    I also had other question,while copying code here, how do i copy line numbers ?
    thanks
    sedy

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since sz is static, you only get one size, ever, despite the number of lists you create.

    This is silly.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    44
    sz is for updating the number of nodes ,not the number of lists. so if i create node each time, each node or object would share updated size as static. is it not the way it should work?
    and does this cause the pointer acess problem?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That is so amazingly not the way it should work, no. Each list needs to know its own size, so that you can actually build the list. Look what happens when you try to build list b: sz is already 1, hence the if sz==0 is not true, which means you never set head and you try to use tail even though b doesn't have a tail, yet.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    44
    i think I am getting confused. Here, I am trying to build just one list with three elements, 10,20 30 which are passed through objects a,b,c. so, when sz is 1, I am adding element to first element of the same list, not creating a new list from start. is it not the way it I should build a single list?
    This has worked for me in C, but I am learning C++, so I might be implementing it wrong.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's not how you did/do it in C either. You might have three nodes, but not three separate lists. (And even in C you probably didn't.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List / Dynamic memory
    By mrsirpoopsalot in forum C++ Programming
    Replies: 21
    Last Post: 05-26-2009, 04:26 AM
  2. Access denied on adding another registry value
    By BobS0327 in forum Windows Programming
    Replies: 0
    Last Post: 01-09-2006, 08:10 PM
  3. c:\boot.ini access denied!
    By Bajanine in forum Tech Board
    Replies: 8
    Last Post: 11-17-2005, 11:52 AM
  4. access denied
    By laasunde in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2002, 02:38 PM
  5. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 02:02 PM

Tags for this Thread