Thread: Why Do i get NULL pointer assignment In this program?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    Why Do i get NULL pointer assignment In this program?

    Hello,
    i created a program for polynomial addition which adds 2 polynomials by way of link list. But when i create 2 linklist and add them everything goes right and then when i press 4 for exit i keep getting "NULL pointer assignment" i have deleted all the memory that i created with new but still why??Please help me. Here is the source code

    Code:
    #include <iostream.h>
    #include <stdio.h>
    class poly
    {
      struct node
      {
    	float co;
    	int power;
    	struct node *next;
      }*first;
      public:
    	poly()
    	{
    
    		first=NULL;
    	}
    	void getdata();
    	void putdata();
    	void add(poly );
    	void del();
    };
    void poly::del()
    {
      struct node *q;
      while(first!=NULL)
      {
    	q=first;
    	first=first->next;
    	delete q;
      }
    
    }
    void poly::add(poly b)
    {
     struct node *q=first,*t=b.first,*ans=NULL,*m,*newnode;
     while(q!=NULL && t!=NULL)
     {
       newnode=new node;
       newnode->next=NULL;
       if(q->power>t->power)
       {
    	newnode->co=q->co;
    	newnode->power=q->power;
    	q=q->next;
       }
       else if(q->power<t->power)
       {
    	newnode->co=t->co;
    	newnode->power=t->power;
    	t=t->next;
       }
       else
       {
    	newnode->co=t->co+q->co;
    	newnode->power=t->power;
    	t=t->next;
    	q=q->next;
       }
       if(ans==NULL)
    	ans=newnode;
       else
    	m->next=newnode;
    	m=newnode;
     }
     while(q!=NULL)
     {
    	newnode=new node;
    	newnode->co=q->co;
    	newnode->power=q->power;
    	newnode->next=NULL;
    	q=q->next;
     }
     while(t!=NULL)
     {
    	newnode=new node;
    	newnode->co=t->co;
    	newnode->power=t->power;
    	newnode->next=NULL;
    	t=t->next;
     }
     q=ans;
     cout<<endl;
     while(q!=NULL)
     {
    	cout<<q->co<<" X"<<q->power<<" +";
    	q=q->next;
     }
     cout<<"\b";
     cout<<NULL;
    
     while(ans!=NULL)
     {
                q=ans;
    	ans=ans->next;
    	delete q;
     }
    }
    void poly::getdata()
    {
     struct node *q=first,*newnode;
     float tco;
     int tpower;
     while(first!=NULL)
     {
    	first=first->next;
    	delete q;
     }
     cout<<"\n\nEnter Co-ordinate (Enter -1 To Exit) :";
     cin>>tco;
     while(tco!=-1)
     {
    	cout<<"\n\nEnter Power :";
    	cin>>tpower;
    	newnode=new node;
    	newnode->co=tco;
    	newnode->power=tpower;
    	newnode->next=NULL;
    	if(first==NULL || newnode->power>first->power)
    	{
    		newnode->next=first;
    		first=newnode;
    	}
    	else
    	{
    		q=first;
    		while(q!=NULL)
    		{
    			if(q->power>=newnode->power && (q->next->power<newnode->power || q->next==NULL))
    			{
    				newnode->next=q->next;
    				q->next=newnode;
    				break;
    			}
    			q=q->next;
    		}
    	}
    		 cout<<"\n\nEnter Co-ordinate (Enter -1 To Exit) :";
    		 cin>>tco;
    
     }
    }
    void poly::putdata()
    {
     struct node *q=first;
     while(q!=NULL)
     {
    	cout<<q->co<<"X"<<q->power<<" +";
    	q=q->next;
     }
     cout<<"\b";
     putch(NULL);
    }
    
    int main(void)
    {
     poly a,b;
     int ch;
    do
    {
     cout<<"\n\nAddition Of 2 Polynomials";
     cout<<"\n\n1 :Enter 1st Polynomial";
     cout<<"\n\n2 :Enter 2nd Polynomial";
     cout<<"\n\n3 :Addition Of 2 Polynomials";
     cout<<"\n\n4 :Exit";
     cout<<"\n\nEnter Your Choice :";
     cin>>ch;
     switch(ch)
     {
    	case 1:
    	a.getdata();
    	a.putdata();
    	break;
    	case 2:
    	b.getdata();
    	b.putdata();
    	break;
    	case 3:
    	a.add(b);
    	break;
    	case 4:
    		cout<<"\n\nBye-Bye";
    	break;
    	default:
    		cout<<"\n\nWrong Choice";
     }
    
    }while(ch!=4);
    a.del();
    b.del();
    
     return 0;
    }
    Last edited by chottachatri; 04-08-2008 at 05:17 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    	poly()
    	{
    		first->co=first->power=0;
    		first=NULL;
    	}
    The red line is illegal because there's no memory allocated for the pointer first. You should be getting an access violation, hopefully.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok elysia that i did. But still i am getting the same problem the program runs perfectly but when i press 4 for exit..i get NULL pointer assignment. In which line do you think can be the problem?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    void poly::del()
    {
      struct node *q=first;
      while(first!=NULL)
      {
    	first=first->next;
    	delete q;
      }
    
    }
    Walk through what this code does - do you notice anything wrong.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Oops! Ok matsp i am posting the new code only. Don't know how did i do that silly mistake. Ok but after correcting also still i am getting the same error. Now which line do you think can the problem be?

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok matsp Now the problem is solved. The same problem was there at another place i solved it and now it works fine

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. addrinfo Structure Not Working
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 10-22-2008, 10:07 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. When do we get Null Pointer Assignment
    By YALINI in forum C Programming
    Replies: 1
    Last Post: 08-29-2001, 01:48 AM