sometimes crashes

J

jw

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

char
*words[53]={"come","garden","one","nice","go","we","some","where","old",
"days","who","bilir","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","ice", "look",
"hand","cry","blood","goes","war",
"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,*TOP;
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=NULL;

}


void getWords();
void displayAll();

};


void list::displayAll()
{

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(node *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.getWords();
thelist.displayAll();
}
 
V

Victor Bazarov

jw said:
//it sometimes crashes sometimes not why?
[...]

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
 
A

Axter

jw said:
//it sometimes crashes sometimes not why?
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<ctime>
using namespace std;
#ifndef NULL
#define NULL (short) 0
#endif

char
*words[53]={"come","garden","one","nice","go","we","some","where","old",
"days","who","bilir","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","ice", "look",
"hand","cry","blood","goes","war",
"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,*TOP;
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=NULL;

}


void getWords();
void displayAll();

};


void list::displayAll()
{

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(node *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.getWords();
thelist.displayAll();
}
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","garden","one","nice","go","we","some","where","old",

"days","who","bilir","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","ice", "look",
"hand","cry","blood","goes","war",
"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()%MaxNum);
 
H

Howard

jw said:
//it sometimes crashes sometimes not why?
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<ctime>
using namespace std;
#ifndef NULL
#define NULL (short) 0
#endif

char
*words[53]={"come","garden","one","nice","go","we","some","where","old",
"days","who","bilir","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","ice", "look",
"hand","cry","blood","goes","war",
"loop","open","pass","throw","age","monkey","doom","hobaa"};

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
 
K

Kai-Uwe Bux

jw said:
//it sometimes crashes sometimes not why?
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<ctime>
using namespace std;
#ifndef NULL
#define NULL (short) 0
#endif

char
*words[53]={"come","garden","one","nice","go","we","some","where","old",
"days","who","bilir","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","ice", "look",
"hand","cry","blood","goes","war",
"loop","open","pass","throw","age","monkey","doom","hobaa"};

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

[snip]


Best

Kai-Uwe Bux
 
V

Victor Bazarov

Kai-Uwe Bux said:
jw wrote:

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

char
*words[53]={"come","garden","one","nice","go","we","some","where","old",

"days","who","bilir","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","ice", "look",
"hand","cry","blood","goes","war",
"loop","open","pass","throw","age","monkey","doom","hobaa"};


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

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

V
 
A

Axter

Axter said:
jw said:
//it sometimes crashes sometimes not why?
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<ctime>
using namespace std;
#ifndef NULL
#define NULL (short) 0
#endif

char
*words[53]={"come","garden","one","nice","go","we","some","where","old",
"days","who","bilir","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","ice", "look",
"hand","cry","blood","goes","war",
"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,*TOP;
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=NULL;

}


void getWords();
void displayAll();

};


void list::displayAll()
{

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(node *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.getWords();
thelist.displayAll();
}
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","garden","one","nice","go","we","some","where","old",

"days","who","bilir","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","ice", "look",
"hand","cry","blood","goes","war",
"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()%MaxNum);

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.
 
O

Old Wolf

Axter said:
jw said:
//it sometimes crashes sometimes not why?
char
*words[53]={"come","garden","one","nice","go","we","some","where","old",
"days","who","bilir","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","ice", "look",
"hand","cry","blood","goes","war",
"loop","open","pass","throw","age","monkey","doom","hobaa"};

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.

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 } .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Infinite loop problem 1
need help with link list 7
doubly linked list 0
need help 3
rand() 16
Deleting first element of a linked list 4
sorting the nodes 6
compilation error: undefined reference (g++ on Linux) 2

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top