C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

sometimes crashes

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
jw
Guest





PostPosted: Mon Nov 28, 2005 7:03 pm    Post subject: sometimes crashes Reply with quote



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

//*****************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"< }
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();
}

Back to top
Victor Bazarov
Guest





PostPosted: Mon Nov 28, 2005 7:27 pm    Post subject: Re: sometimes crashes Reply with quote



jw wrote:
Quote:
//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

Back to top
Axter
Guest





PostPosted: Mon Nov 28, 2005 7:31 pm    Post subject: Re: sometimes crashes Reply with quote




jw wrote:
Quote:
//it sometimes crashes sometimes not why?
#include<iostream
#include #include #include #include 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< }
};

//*****************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"< }
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);


Back to top
Howard
Guest





PostPosted: Mon Nov 28, 2005 7:34 pm    Post subject: Re: sometimes crashes Reply with quote


"jw" <jackwht (AT) gmail (DOT) com> wrote

Quote:
//it sometimes crashes sometimes not why?
#include #include #include #include #include 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



Back to top
Kai-Uwe Bux
Guest





PostPosted: Mon Nov 28, 2005 7:43 pm    Post subject: Re: sometimes crashes Reply with quote

jw wrote:

Quote:
//it sometimes crashes sometimes not why?
#include #include #include #include #include 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

Back to top
Victor Bazarov
Guest





PostPosted: Mon Nov 28, 2005 7:56 pm    Post subject: Re: sometimes crashes Reply with quote

Kai-Uwe Bux wrote:
Quote:
jw wrote:


//it sometimes crashes sometimes not why?
#include #include #include #include #include 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

Back to top
Axter
Guest





PostPosted: Mon Nov 28, 2005 8:01 pm    Post subject: Re: sometimes crashes Reply with quote


Axter wrote:
Quote:
jw wrote:
//it sometimes crashes sometimes not why?
#include<iostream
#include #include #include #include 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< }
};

//*****************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"< }
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.


Back to top
Old Wolf
Guest





PostPosted: Mon Nov 28, 2005 11:51 pm    Post subject: Re: sometimes crashes Reply with quote

Axter wrote:
Quote:
jw wrote:
//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 } .


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.