hi below is code it is only a part of my program simplified for understanding the main problem i am facing ....................

actually the code runs well but while exiting from main displays Null pointer assignment . May i please request the forum member to debug the program..... the code is given below

I am using Turbo c++ version 3.0 compiler

#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;

}

1. Your indentation sucks - no wonder you can't find anything wrong with it, it's way too hard to trace your way through it.

This is some improvement, but it could still be better.

#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;
}

> #include<iostream.h>
This has been obsolete C++ for over a decade

> #include<process.h>
This has been an obsolete OS header for longer

> #include<stdio.h>
This is a C header file.

3. main returns an int, not void.

> i>>x.p;
4. First obvious problem - you never allocate p

I agree with the above reply that indentation and a couple of comments would make it easier to look over ...you would get more assistance.
I also agree with the previous reply that you never allocated any memory for p.

In addition, I think you will get a problem in the destructor

while(tmp!=NULL)    {      
     tmp=tmp->next;      
     delete(tmp->previous);    
}   
 head=tmp;    
delete(p);

build a list with just one or two nodes and think about what the above code will do when there is only one node left. you changed the value of tmp and it is possible that tmp->next is null!

you could do the following

while(tmp != NULL) {
     list* tmp2=tmp->next;    // save tmp->next
    delete(tmp);   // delete this node
    tmp = tmp2;   // move on to the next node (if any)
}

I agree with the above reply that indentation and a couple of comments would make it easier to look over ...you would get more assistance.
I also agree with the previous reply that you never allocated any memory for p.

In addition, I think you will get a problem in the destructor

while(tmp!=NULL)    {      
     tmp=tmp->next;      
     delete(tmp->previous);    
}   
 head=tmp;    
delete(p);

build a list with just one or two nodes and think about what the above code will do when there is only one node left. you changed the value of tmp and it is possible that tmp->next is null!

you could do the following

while(tmp != NULL) {
     list* tmp2=tmp->next;    // save tmp->next
    delete(tmp);   // delete this node
    tmp = tmp2;   // move on to the next node (if any)
}

thanks for destructor suggestion ....... I would be happy if you could tell how can i allocate to p when the size of the input string is unknow..... please fell free to suggest modification in code

1. Your indentation sucks - no wonder you can't find anything wrong with it, it's way too hard to trace your way through it.

This is some improvement, but it could still be better.

#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;
}

> #include<iostream.h>
This has been obsolete C++ for over a decade

> #include<process.h>
This has been an obsolete OS header for longer

> #include<stdio.h>
This is a C header file.

3. main returns an int, not void.

> i>>x.p;
4. First obvious problem - you never allocate p

Please suggest how can i allocate memory to p when i don't know the size of input string

Please suggest how can i allocate memory to p when i don't know the size of input string

I was thinking maybe you could just set that field to char ..since that is what you eventually put into it.

In the function where you actually prompt have the user enter a number you could just save into a string (char array) with some maximum input...that is what I would do.

I hope I am remembering the code correctly from yesterday but those were the thoughts that occurred to me.

BTW,
when I was going over your program (and testing) I just used malloc and set aside 100 bytes...but you have to remember to release ...but to be honest it seemed like a waste since you only really use 1 char in each node.

This article has been dead for over six months. Start a new discussion instead.