Null pointer assignment

Discussion in 'C++' started by rajdey1, Mar 14, 2010.

  1. rajdey1

    rajdey1 New Member

    Joined:
    Mar 14, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi The program given below runs well does the required but prints on output screen Null pointer assignment while exiting from main ......
    Please help me to debug the program.......... Just to mention it is only a part of program pasted to make things simple to understand and bug lies here since this part of the program when compiled and run prints on screen Null pointer assignment.... else does well

    I am using Turbo C++ version 3.0 compiler

    Code:
    #include<iostream.h>
    #include<process.h>
    #include<stdio.h>
    struct list
           {int data;
        list *next;
        list *previous;
           };
    
    
    class integer
    {
      char *p;
    
      list *head;
    
    public:
       integer()
           {head=NULL;
           p=NULL;
           }
       ~integer()
        {list *tmp=head;
         while(tmp!=NULL)
              {tmp=tmp->next;
               delete(tmp->previous);
               }
         head=tmp;
         delete(p);
    
         }
    
    friend ostream& operator <<(ostream &, integer &);
    friend istream& operator >>(istream &,integer &);
    integer& operator +(integer&);
    
    };
    
    ostream& operator << (ostream& o,integer& x)
       { list *tmp=x.head;
    
         if(tmp==NULL)
         { o<<"Integer Has Not Been Assigned Any Value"<<endl;
           return o;
         }
    
         while(tmp!=NULL)
         { o<<tmp->data;
           tmp=tmp->next;
          }
    
         return o;
    
      }
    
    istream& operator >> (istream& i,integer& x)
           {list *tmp=x.head;
        char *k=NULL;
         i>>x.p;
         k=x.p;
         if(tmp!=NULL)
        { while(tmp->next!=NULL)
         tmp=tmp->next;
         while(tmp->previous!=NULL)
         {tmp=tmp->previous;
          delete(tmp->next);
         }
         delete(x.head);
        x.head=NULL;
         }
        while(*k!='\0')
             {if(x.head==NULL)
             {
             tmp=new(list);
             if(tmp==NULL)
             {cout<<"No Memory For Allocation"<<endl;
             exit(0);
             }
             tmp->previous=NULL;
             tmp->next=NULL;
              x.head=tmp;
              tmp->data=*k - 48;
             k++;
              }
             else
             {
               tmp->next=new(list);
            if(tmp==NULL)
             {cout<<"No Memory For Allocation"<<endl;
             exit(0);
             }
               tmp->next->previous=tmp;
               tmp=tmp->next;
               tmp->next=NULL;
               tmp->data=*k - 48;
               k++;
             }
             }
     return i;
    
    
    }
    
    void main(void)
    {integer k;
    cin>>k;
    cout<<k;
    
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice