By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,695 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,695 IT Pros & Developers. It's quick & easy.

segmentation fault

P: n/a
can anyone tell me why the following piece of code causes a segmentation fault please ??
char *getFNFromIden(char *ident)
{
int i = 0 ;
printf("identifier %s\n", allMenus[i].identifier) ;
while(allMenus[i].identifier[0] != '\0')
{
printf("Hello World") ;
i++ ;
}
}

As far as i can tell the seg fault is happening at while(bla) as the first printf is working, printing out what is in allmenus[i].identifier.
Thanks for any help
Matt
Nov 13 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
On Tue, 29 Jul 2003 12:21:26 +0100
Matthew Jakeman <ma************@yahoo.co.uk> wrote:
can anyone tell me why the following piece of code causes a segmentation fault
please ??
char *getFNFromIden(char *ident)
{
int i = 0 ;
printf("identifier %s\n", allMenus[i].identifier) ; What's an allMenus? What if allMenus[0] doesn't exist? Is it's identifier field
defined? IS it even a string? We don't know that!
while(allMenus[i].identifier[0] != '\0') what if allMenus[i] is out of bounds? There's no check for that.. And what is
identifier itself is null? Are you certain that the'identifier' of the last
element is empty? What if it isn't? There's no bounds checking whatsoever.
{
printf("Hello World") ;
i++ ;
}
}

Why is the ident argument not used? Where is the code that actually matters?

In other words, there's not enough information here to distill an answer that is
useful for you, unless you found my remarks useful. Give us more.

--
main(int c,char*k,char*s){c>0?main(0,"adceoX$_k6][^hn","-7\
0#05&'40$.6'+).3+1%30"),puts(""):*s?c=!c?-*s:(putchar(45),c
),putchar(main(c,k+=*s-c*-1,s+1)):(s=0);return!s?10:10+*k;}
Nov 13 '05 #2

P: n/a
On Tue, 29 Jul 2003 12:48:14 +0200
Pieter Droogendijk <gi*@binky.homeunix.org> wrote:
On Tue, 29 Jul 2003 12:21:26 +0100
Matthew Jakeman <ma************@yahoo.co.uk> wrote:
can anyone tell me why the following piece of code causes a segmentation fault
please ??
char *getFNFromIden(char *ident)
{
int i = 0 ;
printf("identifier %s\n", allMenus[i].identifier) ; What's an allMenus? What if allMenus[0] doesn't exist? Is it's identifier field
defined? IS it even a string? We don't know that!


allMenus is a sstruct and identifier is a char *.
while(allMenus[i].identifier[0] != '\0')

what if allMenus[i] is out of bounds? There's no check for that.. And what is
identifier itself is null? Are you certain that the'identifier' of the last
element is empty? What if it isn't? There's no bounds checking whatsoever.


allMenus is not out of bounds and identifier is not null because as i said the printf displays its contents, i haven't done any bounds checking yet as i know exactly what is contained in all of the structs and i just want to get the main body of the code working first.
{
printf("Hello World") ;
i++ ;
}
}

Why is the ident argument not used? Where is the code that actually matters?


Argument isnt used as i want to fix this problem before i write the rest of the function, there is no other code in, this is it.
In other words, there's not enough information here to distill an answer that is
useful for you, unless you found my remarks useful. Give us more.

--
main(int c,char*k,char*s){c>0?main(0,"adceoX$_k6][^hn","-7\
0#05&'40$.6'+).3+1%30"),puts(""):*s?c=!c?-*s:(putchar(45),c
),putchar(main(c,k+=*s-c*-1,s+1)):(s=0);return!s?10:10+*k;}

Thanks again.
Matt
Nov 13 '05 #3

P: n/a
Matthew Jakeman wrote:
can anyone tell me why the following piece of code causes a segmentation
fault please ??
There's probably a bad pointer in your data.
char *getFNFromIden(char *ident)
{
int i = 0 ;
printf("identifier %s\n", allMenus[i].identifier) ;
We don't know what the declaration of allMenus is, nor what
its contents are, because YOU HAVEN'T SHOWN US. We're reduced
to guessing.
while(allMenus[i].identifier[0] != '\0')
How many elements does allMenus have? What value of `i` does
the loop die on? Can you prove that allMenus[i] has a valid
non-null pointer at this value of `i`?
{
printf("Hello World") ;
i++ ;
}
}


What is your test code? How will you know if this function works?

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #4

P: n/a
On Tue, 29 Jul 2003 13:31:40 +0100, Matthew Jakeman wrote:
On Tue, 29 Jul 2003 12:48:14 +0200
Pieter Droogendijk <gi*@binky.homeunix.org> wrote:
What's an allMenus? What if allMenus[0] doesn't exist? Is it's
identifier field defined? IS it even a string? We don't know that!


allMenus is a sstruct and identifier is a char *.


Argument isnt used as i want to fix this problem before i write the rest
of the function, there is no other code in, this is it.


Show us the calling code and the initialization of allMenus.

regards
NPV
Nov 13 '05 #5

P: n/a
On Tue, 29 Jul 2003 13:31:40 +0100
Matthew Jakeman <ma************@yahoo.co.uk> wrote:
On Tue, 29 Jul 2003 12:48:14 +0200
Pieter Droogendijk <gi*@binky.homeunix.org> wrote:
On Tue, 29 Jul 2003 12:21:26 +0100
What's an allMenus? What if allMenus[0] doesn't exist? Is it's identifier
field defined? IS it even a string? We don't know that!


allMenus is a sstruct and identifier is a char *.


How is it declared? Is it static? Dynamic? How is it defined? What does it
contain?
what if allMenus[i] is out of bounds? There's no check for that.. And what
is identifier itself is null? Are you certain that the'identifier' of the
last element is empty? What if it isn't? There's no bounds checking
whatsoever.


allMenus is not out of bounds and identifier is not null because as i said the
printf displays its contents, i haven't done any bounds checking yet as i know
exactly what is contained in all of the structs and i just want to get the
main body of the code working first.


the printf only uses allMenus[0]. And how many are there? I'd assume more, but
still I've seen nothing of the definition of allMenus. For all I know,
allMenus[1].identifier, or even allMenus[1] could be undefined.
Why is the ident argument not used? Where is the code that actually matters?


Argument isnt used as i want to fix this problem before i write the rest of
the function, there is no other code in, this is it.


There is other code. This alone would never compile, would it? I don't even know
what the main datatype here, allMenus, looks like, or what it contains.
In order to get help, you'll have to post the declaration and definition of
allMenus, including everything it contains (or as little as possible to make the
program crash), and a call to getFNFromIden. In other words, the rest of the
code. Like I said.
--
main(int c,char*k,char*s){c>0?main(0,"adceoX$_k6][^hn","-7\
0#05&'40$.6'+).3+1%30"),puts(""):*s?c=!c?-*s:(putchar(45),c
),putchar(main(c,k+=*s-c*-1,s+1)):(s=0);return!s?10:10+*k;}
Nov 13 '05 #6

P: n/a
On Tue, 29 Jul 2003 11:58:16 GMT
"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote:
On Tue, 29 Jul 2003 13:31:40 +0100, Matthew Jakeman wrote:
On Tue, 29 Jul 2003 12:48:14 +0200
Pieter Droogendijk <gi*@binky.homeunix.org> wrote:


What's an allMenus? What if allMenus[0] doesn't exist? Is it's
identifier field defined? IS it even a string? We don't know that!


allMenus is a sstruct and identifier is a char *.


Argument isnt used as i want to fix this problem before i write the rest
of the function, there is no other code in, this is it.


Show us the calling code and the initialization of allMenus.

regards
NPV


Here's the struct :

struct menuDetail
{
char *identifier ;
char *name ;
char *ansfile ;
char *ascfile ;
char *keys[] ;
};
struct menuDetail allMenus[128] ;

Here's where it is initialised :

while(fLines[i] != NULL)
{
lToken = strtok(fLines[i], " ") ;
if(strcmp(lToken, "MENU") == 0)
{
lToken = strtok(NULL, "\r\n") ;
allMenus[noMenus].identifier = lToken ;
noMenus++ ;
//Need a better way to store the menu information or a way to map
//the menu number to the menu name
printf("noMenus : %i\nMenu Identifier : %s\n", noMenus , allMenus[noMenus].identifier) ;
i++ ;
The loop then goes on to do a load of other stuff that i think is irrelelevent..

And here's the code that i am having a problem with :

char *getFNFromIden(char *ident)
{
int i = 0 ;
printf("identifier %s\n", allMenus[i].identifier) ;
while(allMenus[i].identifier[0] != '\0')
{
printf("going through the loop again") ;
i++ ;
}
}

Thanks
Matt
Nov 13 '05 #7

P: n/a
On Tue, 29 Jul 2003 14:13:07 +0100
Matthew Jakeman <ma************@yahoo.co.uk> wrote:
On Tue, 29 Jul 2003 11:58:16 GMT
"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote:
On Tue, 29 Jul 2003 13:31:40 +0100, Matthew Jakeman wrote:
On Tue, 29 Jul 2003 12:48:14 +0200
Pieter Droogendijk <gi*@binky.homeunix.org> wrote:


> What's an allMenus? What if allMenus[0] doesn't exist? Is it's
> identifier field defined? IS it even a string? We don't know that!

allMenus is a sstruct and identifier is a char *.


Argument isnt used as i want to fix this problem before i write the rest
of the function, there is no other code in, this is it.


Show us the calling code and the initialization of allMenus.

regards
NPV


Here's the struct :

struct menuDetail
{
char *identifier ;
char *name ;
char *ansfile ;
char *ascfile ;
char *keys[] ;
};
struct menuDetail allMenus[128] ;

Here's where it is initialised :

while(fLines[i] != NULL)
{
lToken = strtok(fLines[i], " ") ;
if(strcmp(lToken, "MENU") == 0)
{
lToken = strtok(NULL, "\r\n") ;
allMenus[noMenus].identifier = lToken ;
noMenus++ ;
//Need a better way to store the menu information or a way to map
//the menu number to the menu name
printf("noMenus : %i\nMenu Identifier : %s\n", noMenus , allMenus[noMenus].identifier) ;
i++ ;
The loop then goes on to do a load of other stuff that i think is irrelelevent..

And here's the code that i am having a problem with :

char *getFNFromIden(char *ident)
{
int i = 0 ;
printf("identifier %s\n", allMenus[i].identifier) ;
while(allMenus[i].identifier[0] != '\0')
{
printf("going through the loop again") ;
i++ ;
}
}

Thanks
Matt


I might not have mentioned that the printf inside the loop never displays anything, so it must exit the first time the while loop statement is evaluated!!
Nov 13 '05 #8

P: n/a
In <20************************************@yahoo.co.u k> Matthew Jakeman <ma************@yahoo.co.uk> writes:
can anyone tell me why the following piece of code causes a segmentation fault please ??
char *getFNFromIden(char *ident)
{
int i = 0 ;
printf("identifier %s\n", allMenus[i].identifier) ;
while(allMenus[i].identifier[0] != '\0')
{
printf("Hello World") ;
i++ ;
}
}

As far as i can tell the seg fault is happening at while(bla) as the first printf is working, printing out what is in allmenus[i].identifier.


Something as simple and "insignificant" as appending a newline character
to the the "Hello World" string will give you an idea about the value of i
by the time the program crashes.

An even better idea is to print the value of i inside the loop, instead of
"Hello World", again followed by a newline character. This information,
combined with a perusal of the code that initialises the array of
structs, should point you to the reason of the segmentation fault.

Writing C code is easy. Figuring out why it doesn't work as intended is
a bit more difficult, but, without this skill, no one can become a C
programmer.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #9

P: n/a
On Tue, 29 Jul 2003 14:13:07 +0100, Matthew Jakeman wrote:
Here's the struct :

struct menuDetail
{
char *identifier ;
char *name ;
char *ansfile ;
char *ascfile ;
char *keys[] ;
};
struct menuDetail allMenus[128] ;

Here's where it is initialised :

while(fLines[i] != NULL)
{
lToken = strtok(fLines[i], " ") ;
if(strcmp(lToken, "MENU") == 0)
{
lToken = strtok(NULL, "\r\n") ;
This may be where it all goes wrong, strtok might return NULL
allMenus[noMenus].identifier = lToken ;
Which is then stored into the identifier
noMenus++ ;
//Need a better way to store the menu information or a way to map
//the menu number to the menu name
printf("noMenus : %i\nMenu Identifier : %s\n", noMenus , allMenus[noMenus].identifier) ;
i++ ;
The loop then goes on to do a load of other stuff that i think is irrelelevent..

And here's the code that i am having a problem with :

char *getFNFromIden(char *ident)
{
int i = 0 ;
printf("identifier %s\n", allMenus[i].identifier) ;
while(allMenus[i].identifier[0] != '\0')
And then you dereference that null pointer.
{
printf("going through the loop again") ;
i++ ;
}
}

Thanks
Matt


Try to insert:

assert(lToken != NULL);

where my first comment was to see if this is the case.

or you could replace the final check with:

while (allMenus[i].identifier && allMenus[i].identifier[0] != '\0')

You don't tell where flines comes from, but if you're reading from a file
the last line isn't guaranteed to contain a newline. (depending on
who/what created the file)

hth
NPV

Nov 13 '05 #10

P: n/a
On Tue, 29 Jul 2003 14:14:37 +0100
Matthew Jakeman <ma************@yahoo.co.uk> wrote:
<snip>
I might not have mentioned that the printf inside the loop never displays
anything, so it must exit the first time the while loop statement is
evaluated!!


Try adding a line terminator to the format string. "Hello World\n" or "Hello
World\r\n" should do the trick. While you're add it, find out how many loops
succeed, like this: printf ("loop count: %d\n", i);

And still we haven't seen any additional code.

--
main(int c,char*k,char*s){c>0?main(0,"adceoX$_k6][^hn","-7\
0#05&'40$.6'+).3+1%30"),puts(""):*s?c=!c?-*s:(putchar(45),c
),putchar(main(c,k+=*s-c*-1,s+1)):(s=0);return!s?10:10+*k;}
Nov 13 '05 #11

P: n/a

"Al Bowers" <xa*@abowers.combase.com> wrote in message
news:bg************@ID-169908.news.uni-berlin.de...


Matthew Jakeman wrote:
On Tue, 29 Jul 2003 11:58:16 GMT
"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote:

On Tue, 29 Jul 2003 13:31:40 +0100, Matthew Jakeman wrote:
On Tue, 29 Jul 2003 12:48:14 +0200
Pieter Droogendijk <gi*@binky.homeunix.org> wrote:
>What's an allMenus? What if allMenus[0] doesn't exist? Is it's
>identifier field defined? IS it even a string? We don't know that!

allMenus is a sstruct and identifier is a char *.

Argument isnt used as i want to fix this problem before i write the restof the function, there is no other code in, this is it.

Show us the calling code and the initialization of allMenus.

regards
NPV

Here's the struct :

struct menuDetail
{
char *identifier ;
char *name ;
char *ansfile ;
char *ascfile ;
char *keys[] ;
};
struct menuDetail allMenus[128] ;

Here's where it is initialised :

while(fLines[i] != NULL)
{
lToken = strtok(fLines[i], " ") ;
if(strcmp(lToken, "MENU") == 0)
{
lToken = strtok(NULL, "\r\n") ;
allMenus[noMenus].identifier = lToken ;

This is not very useful in tracing the problem.

There is no visiable declaration of fLines or its values.

You have not provided the initial value of the variable
noMenus. If this variable is never 0, then the assignment to
allMenus[0].identifier will not occur in this loop. This
may be cause your function getFNFromIden fails because
you are referencing allMenus[0].identifier.

There are other serious defects in this snippit. You do not check
iToken to make sure it does not have the value of NULL before
you use it as an argument in strcmp. You did not check iToken
for NULL before you assigned iToken to allMenus[noMenus].identifier.

noMenus++ ;
//Need a better way to store the menu information or a way to map
//the menu number to the menu name
printf("noMenus : %i\nMenu Identifier : %s\n", noMenus , allMenus[noMenus].identifier) ;

If this is the actual code then the above should/may crash and burn - you
have already incremented noMenus so now allMenus[noMenus].identifier is NULL
i++ ;
The loop then goes on to do a load of other stuff that i think is irrelelevent..
And here's the code that i am having a problem with :

char *getFNFromIden(char *ident)
{
int i = 0 ;
printf("identifier %s\n", allMenus[i].identifier) ;
while(allMenus[i].identifier[0] != '\0')
{
printf("going through the loop again") ;
i++ ;
}
}


--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #12

P: n/a
Here is the entire function that fille the allMenus struct, this should tell you what data is being put in :

void menuCfgParser()
{
FILE *menuFile ;
char *readIn ;
struct stat menuCfgStat ;
char *lToken = malloc(512) ;
int i = 0 ;
char *fLines[8192] ;
int noMenus = 0;
int noKeys = 0 ;
char *toArray ;
char *tempString = malloc(128) ;

menuFile = fopen(mainBbsInfo.menufile, "r") ;
memset(&menuCfgStat,0,sizeof (struct stat));
if((fstat(fileno(menuFile), &menuCfgStat)) == -1)
{
if(debug) perror("fstat() in menuCfgParser() returned -1") ;
}
readIn = malloc(menuCfgStat.st_size);
fread(readIn, menuCfgStat.st_size, 1, menuFile) ;
fclose(menuFile) ;

lToken = strtok(readIn, "\r\n") ;
i = 0 ;
while(lToken != NULL)
{
toArray = trimLeadingWS(lToken) ;
if((toArray[0] != '#') && (strlen(toArray) != 0))
{
fLines[i] = malloc(strlen(toArray)) ;
fLines[i] = toArray ;
lToken = strtok(NULL, "\r\n") ;
i++ ;
}
else
{
lToken = strtok(NULL, "\r\n") ;
}
}
i = 0 ;
while(fLines[i] != NULL)
{
lToken = strtok(fLines[i], " ") ;
if(strcmp(lToken, "MENU") == 0)
{
lToken = strtok(NULL, "\r\n") ;
allMenus[noMenus].identifier = lToken ;
noMenus++ ;
printf("noMenus : %i\nMenu Identifier : %s\n", noMenus , allMenus[noMenus].identifier) ;
i++ ;
if(strcmp(fLines[i], "{") != 0)
{
printf("Error parsing menu config file after MENU %s\n", lToken) ;
exit(0) ;
}
else
{
i++ ;
}
while(strcmp(fLines[i], "}") != 0)
{
lToken = strtok(fLines[i], " ") ;
if(strcasecmp(lToken, "MENU_NAME") == 0)
{
lToken = strtok(NULL, "\"") ;
allMenus[noMenus].name = lToken ;
if(debug) printf("Menu Name : %s\n", allMenus[noMenus].name) ;
}
else if(strcasecmp(lToken, "MENU_FILE_ANS") == 0)
{
lToken = strtok(NULL, "\r\n") ;
allMenus[noMenus].ansfile = lToken ;
if(debug) printf("Menu ANSI File : %s\n", allMenus[noMenus].ansfile) ;
}
else if(strcasecmp(lToken, "MENU_FILE_ASC") == 0)
{
lToken = strtok(NULL, "\r\n") ;
allMenus[noMenus].ascfile = lToken ;
if(debug) printf("Menu ASCII File : %s\n", allMenus[noMenus].ascfile) ;
}
else if(strcasecmp(lToken, "KEY") == 0)
{
lToken = strtok(NULL, " ") ;
strcpy(tempString, lToken) ;
lToken = strtok(NULL, "\r\n") ;
strcat(tempString, lToken) ;
allMenus[noMenus].keys[noKeys] = tempString ;
if(debug) printf("Menus key binding set : %s\n", allMenus[noMenus].keys[noKeys]) ;
noKeys++ ;
}
i++ ;
}
}
i++ ;
}
}

Here is the function that is failing at the while(blah)

char *getFNFromIden(char *ident)
{
int i = 0 ;
printf("identifier %s\n", allMenus[i].identifier) ;
while(allMenus[i].identifier[0] != '\0')
{
printf("going through the loop again") ;
i++ ;
}
}

This function does not print out "going through the loop again" at all so the loop is obviously not being entered

and this is how the function is being called

getFNFromIden(mainBbsInfo.initmenu) ;

mainBbsInfo.initmenu is "main.ans" but this is pretty much irrelevant i think as it does not get used yet

Thanks again
Matt
Nov 13 '05 #13

P: n/a
On Tue, 29 Jul 2003 18:57:51 +0100
Matthew Jakeman <ma************@yahoo.co.uk> wrote:
Here is the entire function that fille the allMenus struct, this should tell
you what data is being put in :

void menuCfgParser()
{
FILE *menuFile ;
char *readIn ;
struct stat menuCfgStat ;
char *lToken = malloc(512) ;
int i = 0 ;
char *fLines[8192] ;
int noMenus = 0;
int noKeys = 0 ;
char *toArray ;
char *tempString = malloc(128) ;

menuFile = fopen(mainBbsInfo.menufile, "r") ;
memset(&menuCfgStat,0,sizeof (struct stat));
if((fstat(fileno(menuFile), &menuCfgStat)) == -1)
{
if(debug) perror("fstat() in menuCfgParser() returned -1") ;
}
readIn = malloc(menuCfgStat.st_size);
malloc could return NULL.
fread(readIn, menuCfgStat.st_size, 1, menuFile) ;
fread may fail. And, you don't terminate the string. It was memset to 0, so
it's terminated in most cases, but what if you read exactly menuCfgStat.st_size
bytes?
fclose(menuFile) ;

lToken = strtok(readIn, "\r\n") ;
Wasn't IToken just malloc'd? where'd that memory go? It's lost forever.
i = 0 ;
while(lToken != NULL)
{
toArray = trimLeadingWS(lToken) ;
if((toArray[0] != '#') && (strlen(toArray) != 0))
{
fLines[i] = malloc(strlen(toArray)) ;
fLines[i] = toArray ;
No way, you did NOT just do that! First you make fLines[i] point to a
just-malloc'd memory, then you point it to 'toArray'. What you probably want, is
fLines[i] = malloc (strlen(toArray)+1);
fLines[i] = strcpy (fLines[i], toArray);
In your case, all elements of fLines point to whatever trimLeadingWS(IToken)
returned, instead of a copy of the string it points to, which is what you
really want.
lToken = strtok(NULL, "\r\n") ;
i++ ;
}
else
{
lToken = strtok(NULL, "\r\n") ;
}
}
i = 0 ;
while(fLines[i] != NULL)
you're screwed if you read exactly 8192 lines. What you could do instead, is
count how many lines you actually read, and use that instead. Learn how to use
for loops too.
{
lToken = strtok(fLines[i], " ") ;
if(strcmp(lToken, "MENU") == 0)
IToken could be NULL
{
lToken = strtok(NULL, "\r\n") ;
Again
allMenus[noMenus].identifier = lToken ;
noMenus++ ;
printf("noMenus : %i\nMenu Identifier : %s\n", noMenus ,
allMenus[noMenus].identifier) ; i++ ;
IToken may have been NULL, so no guarantee this prints out anything you want
if(strcmp(fLines[i], "{") != 0)
{
printf("Error parsing menu config file after MENU %s\n", lToken) ;
exit(0) ;
}
else
{
i++ ;
}
while(strcmp(fLines[i], "}") != 0)
{
lToken = strtok(fLines[i], " ") ;
NULL?
if(strcasecmp(lToken, "MENU_NAME") == 0)
{
lToken = strtok(NULL, "\"") ;
NULL?
allMenus[noMenus].name = lToken ;
if(debug) printf("Menu Name : %s\n", allMenus[noMenus].name) ;
}
else if(strcasecmp(lToken, "MENU_FILE_ANS") == 0)
{
lToken = strtok(NULL, "\r\n") ; NULL? allMenus[noMenus].ansfile = lToken ;
if(debug) printf("Menu ANSI File : %s\n", allMenus[noMenus].ansfile) ;
}
else if(strcasecmp(lToken, "MENU_FILE_ASC") == 0)
{
lToken = strtok(NULL, "\r\n") ; NULL? allMenus[noMenus].ascfile = lToken ;
if(debug) printf("Menu ASCII File : %s\n", allMenus[noMenus].ascfile)
;
}
else if(strcasecmp(lToken, "KEY") == 0)
{
lToken = strtok(NULL, " ") ; NULL? strcpy(tempString, lToken) ;
lToken = strtok(NULL, "\r\n") ; NULL? strcat(tempString, lToken) ; What if it's too big? allMenus[noMenus].keys[noKeys] = tempString ;
All allMenus will be pointing at tempString. Malloc'd once. They'll all be the
same. There's more of these above.
if(debug) printf("Menus key binding set : %s\n",
allMenus[noMenus].keys[noKeys]) ; noKeys++ ;
}
i++ ;
What's this do again?
}
}
i++ ;
Use for loops, seriously. And try naming your counters in nested loops.
}
}

Here is the function that is failing at the while(blah)

char *getFNFromIden(char *ident)
{
int i = 0 ;
printf("identifier %s\n", allMenus[i].identifier) ;
while(allMenus[i].identifier[0] != '\0')
{
printf("going through the loop again") ;
i++ ;
}
}

This function does not print out "going through the loop again" at all so the
loop is obviously not being entered
That's because it segfaults before anything is printed. That's because stdio
prints everything up 'till a newline, the rest sits in the buffer. try:
printf ("went through the loop %d times\n", i);
for some more favorable results. Windows may need you to print \r\n, but i'm not
sure.
and this is how the function is being called

getFNFromIden(mainBbsInfo.initmenu) ;

mainBbsInfo.initmenu is "main.ans" but this is pretty much irrelevant i think
as it does not get used yet

Thanks again
Matt


With everything going wrong in this code, it's no wonder you get a segfault. I
kinda got tired of it at the end there, so I probably missed a few things.
Go fix your code. Better yet, try rewriting it. Try using a better coding style
too.
For loops are good, as is a better naming convention.
Don't rely on the user's capacity to make flawless config files(if that is
indeed what this thing is reading).
Get your pointer galore straightened out.
strcpy() things.
free() is nice.
learn what malloc() does (part of pointer galore lessons)

I'm stopping now, but I could go on for a while.

--
main(int c,char*k,char*s){c>0?main(0,"adceoX$_k6][^hn","-7\
0#05&'40$.6'+).3+1%30"),puts(""):*s?c=!c?-*s:(putchar(45),c
),putchar(main(c,k+=*s-c*-1,s+1)):(s=0);return!s?10:10+*k;}
Nov 13 '05 #14

P: n/a
"Pieter Droogendijk" <gi*@binky.homeunix.org> wrote in message
news:20030729201848.7ba98daf.gi*@binky.homeunix.or g...
On Tue, 29 Jul 2003 18:57:51 +0100
Matthew Jakeman <ma************@yahoo.co.uk> wrote:
Here is the entire function that fille the allMenus struct, this should tell you what data is being put in :

void menuCfgParser()
{
FILE *menuFile ;
char *readIn ;
struct stat menuCfgStat ;
char *lToken = malloc(512) ;
int i = 0 ;
char *fLines[8192] ;
int noMenus = 0;
int noKeys = 0 ;
char *toArray ;
char *tempString = malloc(128) ;

menuFile = fopen(mainBbsInfo.menufile, "r") ;
memset(&menuCfgStat,0,sizeof (struct stat));
if((fstat(fileno(menuFile), &menuCfgStat)) == -1)
{
if(debug) perror("fstat() in menuCfgParser() returned -1") ;
}
readIn = malloc(menuCfgStat.st_size);
malloc could return NULL.
fread(readIn, menuCfgStat.st_size, 1, menuFile) ;


fread may fail. And, you don't terminate the string. It was memset to 0,

so it's terminated in most cases, but what if you read exactly menuCfgStat.st_size bytes?
fclose(menuFile) ;

lToken = strtok(readIn, "\r\n") ;
Wasn't IToken just malloc'd? where'd that memory go? It's lost forever.
i = 0 ;
while(lToken != NULL)
{
toArray = trimLeadingWS(lToken) ;
if((toArray[0] != '#') && (strlen(toArray) != 0))
{
fLines[i] = malloc(strlen(toArray)) ;
fLines[i] = toArray ;


No way, you did NOT just do that! First you make fLines[i] point to a
just-malloc'd memory, then you point it to 'toArray'. What you probably

want, is fLines[i] = malloc (strlen(toArray)+1);
fLines[i] = strcpy (fLines[i], toArray);
In your case, all elements of fLines point to whatever trimLeadingWS(IToken) returned, instead of a copy of the string it points to, which is what you
really want.
lToken = strtok(NULL, "\r\n") ;
i++ ;
}
else
{
lToken = strtok(NULL, "\r\n") ;
}
}
i = 0 ;
while(fLines[i] != NULL)
you're screwed if you read exactly 8192 lines. What you could do instead,

is count how many lines you actually read, and use that instead. Learn how to use for loops too.
{
lToken = strtok(fLines[i], " ") ;
if(strcmp(lToken, "MENU") == 0)
IToken could be NULL
{
lToken = strtok(NULL, "\r\n") ;


Again
allMenus[noMenus].identifier = lToken ;
noMenus++ ;
printf("noMenus : %i\nMenu Identifier : %s\n", noMenus ,
allMenus[noMenus].identifier) ; i++ ;


IToken may have been NULL, so no guarantee this prints out anything you

want
if(strcmp(fLines[i], "{") != 0)
{
printf("Error parsing menu config file after MENU %s\n", lToken) ;
exit(0) ;
}
else
{
i++ ;
}
while(strcmp(fLines[i], "}") != 0)
{
lToken = strtok(fLines[i], " ") ;
NULL?
if(strcasecmp(lToken, "MENU_NAME") == 0)
{
lToken = strtok(NULL, "\"") ;


NULL?
allMenus[noMenus].name = lToken ;
if(debug) printf("Menu Name : %s\n", allMenus[noMenus].name) ;
}
else if(strcasecmp(lToken, "MENU_FILE_ANS") == 0)
{
lToken = strtok(NULL, "\r\n") ;

NULL?
allMenus[noMenus].ansfile = lToken ;
if(debug) printf("Menu ANSI File : %s\n", allMenus[noMenus].ansfile) ;
}
else if(strcasecmp(lToken, "MENU_FILE_ASC") == 0)
{
lToken = strtok(NULL, "\r\n") ;

NULL?
allMenus[noMenus].ascfile = lToken ;
if(debug) printf("Menu ASCII File : %s\n", allMenus[noMenus].ascfile)
;
}
else if(strcasecmp(lToken, "KEY") == 0)
{
lToken = strtok(NULL, " ") ;

NULL?
strcpy(tempString, lToken) ;
lToken = strtok(NULL, "\r\n") ;

NULL?
strcat(tempString, lToken) ;

What if it's too big?
allMenus[noMenus].keys[noKeys] = tempString ;


All allMenus will be pointing at tempString. Malloc'd once. They'll all be

the same. There's more of these above.
if(debug) printf("Menus key binding set : %s\n",
allMenus[noMenus].keys[noKeys]) ; noKeys++ ;
}
i++ ;
What's this do again?
}
}
i++ ;


Use for loops, seriously. And try naming your counters in nested loops.
}
}

Here is the function that is failing at the while(blah)

char *getFNFromIden(char *ident)
{
int i = 0 ;
printf("identifier %s\n", allMenus[i].identifier) ;
while(allMenus[i].identifier[0] != '\0')
{
printf("going through the loop again") ;
i++ ;
}
}

This function does not print out "going through the loop again" at all so the loop is obviously not being entered


That's because it segfaults before anything is printed. That's because

stdio prints everything up 'till a newline, the rest sits in the buffer. try:
printf ("went through the loop %d times\n", i);
for some more favorable results. Windows may need you to print \r\n, but i'm not sure.
and this is how the function is being called

getFNFromIden(mainBbsInfo.initmenu) ;

mainBbsInfo.initmenu is "main.ans" but this is pretty much irrelevant i think as it does not get used yet

Thanks again
Matt
With everything going wrong in this code, it's no wonder you get a

segfault. I kinda got tired of it at the end there, so I probably missed a few things.
Go fix your code. Better yet, try rewriting it. Try using a better coding style too.
For loops are good, as is a better naming convention.
Don't rely on the user's capacity to make flawless config files(if that is
indeed what this thing is reading).
Get your pointer galore straightened out.
strcpy() things.
free() is nice.
learn what malloc() does (part of pointer galore lessons)

I'm stopping now, but I could go on for a while.

--
main(int c,char*k,char*s){c>0?main(0,"adceoX$_k6][^hn","-7\
0#05&'40$.6'+).3+1%30"),puts(""):*s?c=!c?-*s:(putchar(45),c
),putchar(main(c,k+=*s-c*-1,s+1)):(s=0);return!s?10:10+*k;}


As previously stated i am not doing the error checking until the main chunk
of code is finished, i know a user will not make a flawless config file
which is why i have written a program to do that for them.
If anyone else actually has an idea why i might be getting the seg fault i
would be grateful!
Matt
Nov 13 '05 #15

P: n/a
garagerules.fsnet.co.uk wrote:
"Pieter Droogendijk" <gi*@binky.homeunix.org> wrote in message
[lots of helpful stuff]
As previously stated i am not doing the error checking until the main
chunk of code is finished, i know a user will not make a flawless config
file which is why i have written a program to do that for them.
If anyone else actually has an idea why i might be getting the seg fault i
would be grateful!


John Fraggin' Sheridan, garagerules, Pieter's post was stuffed with
places which could have lead to problems with your code. Why don't
youn *fix* them?

And not doing the error checking "until the main chunk of code is
finished" is much like "not doing the safety inspection until the
war is finished". That error checking will expose errors in your
code as well as errors in your data.

Build the thing incrementally and seek always to have a running,
tested, program.

--
Chris "if you ignore advice, advice will ignore you." Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.