sometimes crashes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jw

    #1

    sometimes crashes

    //it sometimes crashes sometimes not why?
    #include<iostre am>
    #include<fstrea m>
    #include<vector >
    #include<string >
    #include<ctime>
    using namespace std;
    #ifndef NULL
    #define NULL (short) 0
    #endif

    char
    *words[53]={"come","garde n","one","nice" ,"go","we","som e","where","old ",
    "days","who","b ilir","where"," school","eat"," apple","look"," ayy","see","eye ","haha",
    "birds","fly"," come", "me", "can","sugar"," laugh","ss", "ad", "asad",
    "digf","you ",
    "know", "burn","ah ahh","desk","ic e", "look",
    "hand","cry","b lood","goes","w ar",
    "loop","open"," pass","throw"," age","monkey"," doom","hobaa"};

    //*************** ****node class********** *********
    class node{
    private:
    string word;
    node *next;
    public:

    node()
    {
    next=NULL;
    }

    friend class list;

    void display()
    {
    cout<<word<<" ";
    }
    };

    //*************** **list class********** ***********
    class list{
    private:
    node *head,*tail,*TO P;
    void append(node *new_node);

    public:
    list()
    {
    TOP=new node;
    head=tail=TOP;
    TOP->next=NULL;
    }

    ~list()
    {
    node *ptr=head;
    while(ptr)
    {
    node *tmp=ptr;
    ptr=ptr->next;
    delete tmp;

    }
    head=tail=TOP=N ULL;

    }


    void getWords();
    void displayAll();

    };


    void list::displayAl l()
    {

    node *py;

    py=head;

    if(py==NULL)
    {
    cout<<"nothing to show"<<endl;
    }
    while(py!=NULL)
    {//while py points sth reasonable
    py->display();
    py=py->next;
    }

    }

    void list::append(no de *ptr){

    if(TOP->next==NULL)//first element
    { TOP->next=ptr;
    head=ptr;
    tail=ptr;
    }
    else
    {

    tail->next=ptr;
    tail=ptr;

    }

    }

    void list::getWords( )
    { int num;
    srand((unsigned )time( NULL ));
    for(int m=0;m<50;m++)
    {

    num=(rand()%53) ;
    node *object;
    object=new node;
    object->word=words[num];
    append(object);
    }
    }




    void main()
    {
    list thelist;
    thelist.getWord s();
    thelist.display All();
    }

  • Victor Bazarov

    #2
    Re: sometimes crashes

    jw wrote:[color=blue]
    > //it sometimes crashes sometimes not why?
    > [...][/color]

    Who's to tell?

    Have you tried using the debugger? If yes, where does it crash? If you
    haven't, why not?

    BTW, you'd be much better off without hard-coding the number of your
    words array as "53" but instead using '(sizeof words / sizeof *words)'.
    You probably run into a wrong index you get from the rand() % 53. Are
    you sure you have exactly 53 words?

    V

    Comment

    • Axter

      #3
      Re: sometimes crashes


      jw wrote:[color=blue]
      > //it sometimes crashes sometimes not why?
      > #include<iostre am>
      > #include<fstrea m>
      > #include<vector >
      > #include<string >
      > #include<ctime>
      > using namespace std;
      > #ifndef NULL
      > #define NULL (short) 0
      > #endif
      >
      > char
      > *words[53]={"come","garde n","one","nice" ,"go","we","som e","where","old ",
      > "days","who","b ilir","where"," school","eat"," apple","look"," ayy","see","eye ","haha",
      > "birds","fly"," come", "me", "can","sugar"," laugh","ss", "ad", "asad",
      > "digf","you ",
      > "know", "burn","ah ahh","desk","ic e", "look",
      > "hand","cry","b lood","goes","w ar",
      > "loop","open"," pass","throw"," age","monkey"," doom","hobaa"};
      >
      > //*************** ****node class********** *********
      > class node{
      > private:
      > string word;
      > node *next;
      > public:
      >
      > node()
      > {
      > next=NULL;
      > }
      >
      > friend class list;
      >
      > void display()
      > {
      > cout<<word<<" ";
      > }
      > };
      >
      > //*************** **list class********** ***********
      > class list{
      > private:
      > node *head,*tail,*TO P;
      > void append(node *new_node);
      >
      > public:
      > list()
      > {
      > TOP=new node;
      > head=tail=TOP;
      > TOP->next=NULL;
      > }
      >
      > ~list()
      > {
      > node *ptr=head;
      > while(ptr)
      > {
      > node *tmp=ptr;
      > ptr=ptr->next;
      > delete tmp;
      >
      > }
      > head=tail=TOP=N ULL;
      >
      > }
      >
      >
      > void getWords();
      > void displayAll();
      >
      > };
      >
      >
      > void list::displayAl l()
      > {
      >
      > node *py;
      >
      > py=head;
      >
      > if(py==NULL)
      > {
      > cout<<"nothing to show"<<endl;
      > }
      > while(py!=NULL)
      > {//while py points sth reasonable
      > py->display();
      > py=py->next;
      > }
      >
      > }
      >
      > void list::append(no de *ptr){
      >
      > if(TOP->next==NULL)//first element
      > { TOP->next=ptr;
      > head=ptr;
      > tail=ptr;
      > }
      > else
      > {
      >
      > tail->next=ptr;
      > tail=ptr;
      >
      > }
      >
      > }
      >
      > void list::getWords( )
      > { int num;
      > srand((unsigned )time( NULL ));
      > for(int m=0;m<50;m++)
      > {
      >
      > num=(rand()%53) ;
      > node *object;
      > object=new node;
      > object->word=words[num];
      > append(object);
      > }
      > }
      >
      >
      >
      >
      > void main()
      > {
      > list thelist;
      > thelist.getWord s();
      > thelist.display All();
      > }[/color]
      You don't have 53 words in your list, so any time it points to the 53
      pointer, it will be pointing to random memory.
      You should avoid using pointers as much as possible. They're not safe,
      and can lead to this type of bug that is hard to track down.
      The following would require a little bit more memory, but would be
      safer and easier to maintain:
      const char
      words[][22]={"come","garde n","one","nice" ,"go","we","som e","where","old ",

      "days","who","b ilir","where"," school","eat"," apple","look"," ayy","see","eye ­","haha",

      "birds","fly"," come", "me", "can","sugar"," laugh","ss", "ad", "asad",
      "digf","you ",
      "know", "burn","ah ahh","desk","ic e", "look",
      "hand","cry","b lood","goes","w ar",
      "loop","open"," pass","throw"," age","monkey"," doom","hobaa"};

      void list::getWords( )
      {
      int num;
      srand((unsigned )time( NULL ));
      for(int m=0;m<50;m++)
      {
      const int MaxNum = sizeof(words) / sizeof(words[0]);
      num=(rand()%Max Num);

      Comment

      • Howard

        #4
        Re: sometimes crashes


        "jw" <jackwht@gmail. com> wrote in message
        news:1133204590 .950873.245790@ o13g2000cwo.goo glegroups.com.. .[color=blue]
        > //it sometimes crashes sometimes not why?
        > #include<iostre am>
        > #include<fstrea m>
        > #include<vector >
        > #include<string >
        > #include<ctime>
        > using namespace std;
        > #ifndef NULL
        > #define NULL (short) 0
        > #endif
        >
        > char
        > *words[53]={"come","garde n","one","nice" ,"go","we","som e","where","old ",
        > "days","who","b ilir","where"," school","eat"," apple","look"," ayy","see","eye ","haha",
        > "birds","fly"," come", "me", "can","sugar"," laugh","ss", "ad", "asad",
        > "digf","you ",
        > "know", "burn","ah ahh","desk","ic e", "look",
        > "hand","cry","b lood","goes","w ar",
        > "loop","open"," pass","throw"," age","monkey"," doom","hobaa"};[/color]

        I count only 52 words specified there.

        What happens when you try to use the item at words[52]? Test it in your
        debugger and find out.

        (Perhaps you meant "ah ahh" to be two separate words?)

        -Howard


        Comment

        • Kai-Uwe Bux

          #5
          Re: sometimes crashes

          jw wrote:
          [color=blue]
          > //it sometimes crashes sometimes not why?
          > #include<iostre am>
          > #include<fstrea m>
          > #include<vector >
          > #include<string >
          > #include<ctime>
          > using namespace std;
          > #ifndef NULL
          > #define NULL (short) 0
          > #endif
          >
          > char
          > *words[53]={"come","garde n","one","nice" ,"go","we","som e","where","old ",
          >[/color]
          "days","who","b ilir","where"," school","eat"," apple","look"," ayy","see","eye ","haha",[color=blue]
          > "birds","fly"," come", "me", "can","sugar"," laugh","ss", "ad", "asad",
          > "digf","you ",
          > "know", "burn","ah ahh","desk","ic e", "look",
          > "hand","cry","b lood","goes","w ar",
          > "loop","open"," pass","throw"," age","monkey"," doom","hobaa"};[/color]

          Count! This list has only 52 elements. Thus word[52] is uninitialized.

          [snip]


          Best

          Kai-Uwe Bux

          Comment

          • Victor Bazarov

            #6
            Re: sometimes crashes

            Kai-Uwe Bux wrote:[color=blue]
            > jw wrote:
            >
            >[color=green]
            >>//it sometimes crashes sometimes not why?
            >>#include<iost ream>
            >>#include<fstr eam>
            >>#include<vect or>
            >>#include<stri ng>
            >>#include<ctim e>
            >>using namespace std;
            >>#ifndef NULL
            >>#define NULL (short) 0
            >>#endif
            >>
            >>char
            >>*words[53]={"come","garde n","one","nice" ,"go","we","som e","where","old ",
            >>[/color]
            >
            > "days","who","b ilir","where"," school","eat"," apple","look"," ayy","see","eye ","haha",
            >[color=green]
            >>"birds","fly" ,"come", "me", "can","sugar"," laugh","ss", "ad", "asad",
            >>"digf","you ",
            >>"know", "burn","ah ahh","desk","ic e", "look",
            >>"hand","cry", "blood","goes", "war",
            >>"loop","open" ,"pass","throw" ,"age","monkey" ,"doom","hobaa" };[/color]
            >
            >
            > Count! This list has only 52 elements. Thus word[52] is uninitialized.[/color]

            No, it's not uninitialised, it's initialised to 0.

            V

            Comment

            • Axter

              #7
              Re: sometimes crashes


              Axter wrote:[color=blue]
              > jw wrote:[color=green]
              > > //it sometimes crashes sometimes not why?
              > > #include<iostre am>
              > > #include<fstrea m>
              > > #include<vector >
              > > #include<string >
              > > #include<ctime>
              > > using namespace std;
              > > #ifndef NULL
              > > #define NULL (short) 0
              > > #endif
              > >
              > > char
              > > *words[53]={"come","garde n","one","nice" ,"go","we","som e","where","old ",
              > > "days","who","b ilir","where"," school","eat"," apple","look"," ayy","see","eye ","haha",
              > > "birds","fly"," come", "me", "can","sugar"," laugh","ss", "ad", "asad",
              > > "digf","you ",
              > > "know", "burn","ah ahh","desk","ic e", "look",
              > > "hand","cry","b lood","goes","w ar",
              > > "loop","open"," pass","throw"," age","monkey"," doom","hobaa"};
              > >
              > > //*************** ****node class********** *********
              > > class node{
              > > private:
              > > string word;
              > > node *next;
              > > public:
              > >
              > > node()
              > > {
              > > next=NULL;
              > > }
              > >
              > > friend class list;
              > >
              > > void display()
              > > {
              > > cout<<word<<" ";
              > > }
              > > };
              > >
              > > //*************** **list class********** ***********
              > > class list{
              > > private:
              > > node *head,*tail,*TO P;
              > > void append(node *new_node);
              > >
              > > public:
              > > list()
              > > {
              > > TOP=new node;
              > > head=tail=TOP;
              > > TOP->next=NULL;
              > > }
              > >
              > > ~list()
              > > {
              > > node *ptr=head;
              > > while(ptr)
              > > {
              > > node *tmp=ptr;
              > > ptr=ptr->next;
              > > delete tmp;
              > >
              > > }
              > > head=tail=TOP=N ULL;
              > >
              > > }
              > >
              > >
              > > void getWords();
              > > void displayAll();
              > >
              > > };
              > >
              > >
              > > void list::displayAl l()
              > > {
              > >
              > > node *py;
              > >
              > > py=head;
              > >
              > > if(py==NULL)
              > > {
              > > cout<<"nothing to show"<<endl;
              > > }
              > > while(py!=NULL)
              > > {//while py points sth reasonable
              > > py->display();
              > > py=py->next;
              > > }
              > >
              > > }
              > >
              > > void list::append(no de *ptr){
              > >
              > > if(TOP->next==NULL)//first element
              > > { TOP->next=ptr;
              > > head=ptr;
              > > tail=ptr;
              > > }
              > > else
              > > {
              > >
              > > tail->next=ptr;
              > > tail=ptr;
              > >
              > > }
              > >
              > > }
              > >
              > > void list::getWords( )
              > > { int num;
              > > srand((unsigned )time( NULL ));
              > > for(int m=0;m<50;m++)
              > > {
              > >
              > > num=(rand()%53) ;
              > > node *object;
              > > object=new node;
              > > object->word=words[num];
              > > append(object);
              > > }
              > > }
              > >
              > >
              > >
              > >
              > > void main()
              > > {
              > > list thelist;
              > > thelist.getWord s();
              > > thelist.display All();
              > > }[/color]
              > You don't have 53 words in your list, so any time it points to the 53
              > pointer, it will be pointing to random memory.
              > You should avoid using pointers as much as possible. They're not safe,
              > and can lead to this type of bug that is hard to track down.
              > The following would require a little bit more memory, but would be
              > safer and easier to maintain:
              > const char
              > words[][22]={"come","garde n","one","nice" ,"go","we","som e","where","old ",
              >
              > "days","who","b ilir","where"," school","eat"," apple","look"," ayy","see","eye ­","haha",
              >
              > "birds","fly"," come", "me", "can","sugar"," laugh","ss", "ad", "asad",
              > "digf","you ",
              > "know", "burn","ah ahh","desk","ic e", "look",
              > "hand","cry","b lood","goes","w ar",
              > "loop","open"," pass","throw"," age","monkey"," doom","hobaa"};
              >
              > void list::getWords( )
              > {
              > int num;
              > srand((unsigned )time( NULL ));
              > for(int m=0;m<50;m++)
              > {
              > const int MaxNum = sizeof(words) / sizeof(words[0]);
              > num=(rand()%Max Num);[/color]

              I forgot to mention that you should also move your seed (srand) to your
              main function.
              Otherwise, you'll find you're not getting true random numbers.
              Typically, you only want to perform the seed once in your application.

              Comment

              • Old Wolf

                #8
                Re: sometimes crashes

                Axter wrote:[color=blue]
                > jw wrote:[color=green]
                > > //it sometimes crashes sometimes not why?
                >> char
                >> *words[53]={"come","garde n","one","nice" ,"go","we","som e","where","old ",
                >> "days","who","b ilir","where"," school","eat"," apple","look"," ayy","see","eye ","haha",
                >> "birds","fly"," come", "me", "can","sugar"," laugh","ss", "ad", "asad",
                >> "digf","you ",
                >> "know", "burn","ah ahh","desk","ic e", "look",
                >> "hand","cry","b lood","goes","w ar",
                >> "loop","open"," pass","throw"," age","monkey"," doom","hobaa"};[/color]
                >
                > You don't have 53 words in your list, so any time it points to the 53
                > pointer, it will be pointing to random memory.[/color]

                Actually it will be a null pointer. Initializing a struct or array
                always
                initializes all members. If there are fewer initializers than members,
                the other members are initialized with { 0 } .

                Comment

                Working...