Menu

dev C++ error when run double linking program

anhtrue
2007-11-09
2012-09-26
  • anhtrue

    anhtrue - 2007-11-09

    i writed a programma :
    and it is source code:

    include <iostream.h>

    include <conio.h>

    class list;

    class node
    {
    private:
    int data;
    node next;
    node
    prev;
    public:
    node ()
    {
    data=0;
    next=NULL;
    prev=NULL;
    }
    node (int newdata)
    {
    data=newdata;
    next=NULL;
    prev=NULL;
    }
    friend class list;
    };

    class list
    {
    private:
    node *head;
    public:
    list():head(0){}

             bool insert_first(int newdata)
             {
                  node  *temp= new node (newdata);
                  if (head==NULL)
                  {
                     head=temp;
                     return 1;
                     }
                  else
                  {
                      head-&gt;prev=temp;
                      temp-&gt;next=head;
                      head=temp;
                      return 1;
                      }
    
                      }
             bool delete_first()
             {
                  if (head==NULL)
                  {
                                 cout&lt;&lt;&quot;no object to delete&quot;&lt;&lt;endl;
                                 return 0;
                                 }
    
                  else
                  {
                      node * temp=head;
                      head=head-&gt;next;
                      head-&gt;prev=NULL;
                      delete temp;
                      return 1;
                      }
    
                      }
                  bool makeempty()
                  {
                       while (head!=0)
                       {
                             delete_first();
                             }
                             return 1;
    
                            }    
                  void display() const
                  {
                       node *temp=head;
                       while (temp!=0)
                       {
                             cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
                             temp=temp-&gt;next;
                             }
    
                             }
    

    };

    int main()
    {
    list alist;
    for (int i=0;i<10;i++)
    alist.insert_first(i);
    alist.display();
    cout<<endl;
    cout<<"call delete_first() function!"<<endl;
    alist.delete_first();
    alist.display();

    cout&lt;&lt;endl;
    cout&lt;&lt;&quot;call makeempty() function!&quot;&lt;&lt;endl;
    alist.makeempty();
    alist.display();
    
    getch();
    return 1;
    

    }

    and it errors when call makeempty() function
    when i run it on borland C++ 3.0 it is ok but Dev it not...
    i don't know why that..
    could you help me to fix it?

    thanks !

    {sorry my english is not very good! :X)

     
    • anhtrue

      anhtrue - 2007-11-14

      thanks for all helps!

      i have just fixed this problem .
      and this is correct code :
      bool delete_first()
      {
      if (head==NULL)
      {
      cout<<"no object to delete"<<endl;
      return 0;
      }

      else
      {
      node * temp=head;
      if (head->next!=0)
      {
      head=head->next;
      head->prev=NULL;
      delete temp;

      }
      else
      {
      head=NULL;
      delete temp;
      }
      }
      }

      i found a simple problem .head is not a node ,head is a point .when list has only point ,head ->prev can not be NULL ,because head doesn't have it ...

      the problem is as simple as i can not find it...
      thanks for all !!!

       
    • Anonymous

      Anonymous - 2007-11-09

      In Borland C++ your code compiles with warnings:

      Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
      p1.c:
      Warning W8027 p1.c 72: Functions containing while are not expanded inline
      Warning W8027 p1.c 82: Functions containing while are not expanded inline
      Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
      !! OK. Compilation C++ (-Vd)

      And run with this output

      9 8 7 6 5 4 3 2 1 0
      call delete_first() function!
      8 7 6 5 4 3 2 1 0
      call makeempty() function!

      After that it gives a fatal error and abort the execution. So, if the GNU gcc compiler gives you a compile-time error, is clever that the Borland one.

       
      • Osito

        Osito - 2007-11-09

        It compiles OK on gcc, it just crashes when you run it. It crashes on the last call to delete_first() in the while loop. If you remove the loop and replace it with eight calls to delete_first() it's fine, and you get a '0' when it displays the list. If you add a ninth call it crashes. It can't delete the final element in the list.

         
        • Soma

          Soma - 2007-11-10

          "It can't delete the final element in the list."

          Wrong. The code is, as virtually always, the problem.

          Clever programmers list to me, but do not follow me.

          Soma

           
          • Osito

            Osito - 2007-11-10

            Sorry, I was just speaking in plain English. We can't all be as clever as you are. If something deletes items from a list one at a time until it gets to the last item then fails, we normal people would say "it can't delete the final element in the list". I was merely trying to point him to the particular function call that caused it to crash for me, namely the call to delete_first() when the displayed list has been reduced to one element. Sheesh.

             
    • Anonymous

      Anonymous - 2007-11-10

      If you are going to reinvent teh wheel by creating your own linked list at least give it a different name that teh one provided in th e standard library!

      You did know that this functionality was part of the standard library already right!? ;-)

      I feel compelled to point out the use of deprecated headers is discouraged (not least by the conspicuous warning message emitted by the compiler when you use them!).

      Clifford

       
    • anhtrue

      anhtrue - 2007-11-11

      thanks for help !!!

      i asked my teacher and my teacher can not expland why that ...
      he only said :"we are human ,and computer is computer ,it is not human so some program u write it can not run right with your mind ...."

      i think he is right...

       
      • Soma

        Soma - 2007-11-11

        Your "teacher" is an idiot.
        The code may compile.
        The code has a bug.
        The executable may run to completion.
        The executable has a major bug.

        Soma

         
      • Anonymous

        Anonymous - 2007-11-11

        Maybe it was lost in translation but your teacher comes accross as a bit of a cod-philosopher. This is not teh time for philosophy. What I imagine he was saying was that teh computer does exactly what you tell it and the trouble is what you are telling it is not what you think you are telling it. Trust me - he is not blaming the computer! ;-)

         
    • Anonymous

      Anonymous - 2007-11-11

      OK enough nonsense.

      In these lines:

      > head=head->next;
      > head->prev=NULL;

      what do you suppose happens if head->next is NULL? Answer: head->prev would be an attempt to dereference a null pointer.

      The following at least prevents it from crashing.

      > head = head->next ;
      > if( head != NULL )
      > {
      > head->prev = NULL;
      > }

      But I still would not like to bet that the code is correct - I haven't looked that hard. The problem becomes obvious if you were to use a debugger. Why don't programming classes ever seem to teach this essential tool!?

      Clifford

       
    • Anonymous

      Anonymous - 2007-11-11

      > when i run it on borland C++ 3.0 it is ok but Dev it not...

      It ran but it was still wrong. The 32bit code generated by Dev-C++ (or rather Mingw/GCC) allows better use of the processors memory protection mechanisms so will trap errors that are ignored in 16 bit code. This is not a good thing, in the 16 bit code you will find out about the error at some point, the trouble is it will be nowhere near the actual cause of the problem, and so will be very hard to debug. But you need the debugger in the first instance to tell you exactly where the run-time error occurred. Unfortunately Dev-C++'s debugger sucks - I used MS VC++2005 Express.

       
    • anhtrue

      anhtrue - 2007-11-13

      i even didn't know why i'm wrong .
      and this is my think...
      <a href="http://photobucket.com" target="_blank"><img src="http://i117.photobucket.com/albums/o48/anhtrue/test.jpg" border="0" alt="Photo Sharing and Video Hosting at Photobucket"></a>

       
    • anhtrue

      anhtrue - 2007-11-13

      i even didn't know why i'm wrong .
      and this is my think...
      <a href="http://photobucket.com" target="_blank"><img src="http://i117.photobucket.com/albums/o48/anhtrue/test.jpg" border="0" alt="Photo Sharing and Video Hosting at Photobucket"></a>

       
    • anhtrue

      anhtrue - 2007-11-13

      sorry my first image some wrong ,if u can ,please fix it for me ,thanks

       
    • Anonymous

      Anonymous - 2007-11-13

      You have already been given a fix. You are not listening!

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.