segmentation fault

P

Pieter Droogendijk

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.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.identifier[0] != '\0')

what if allMenus 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.
 
M

Matthew Jakeman

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.identifier) ;
while(allMenus.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.identifier.
Thanks for any help
Matt
 
C

Chris Dollin

Matthew said:
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.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.identifier[0] != '\0')


How many elements does allMenus have? What value of `i` does
the loop die on? Can you prove that allMenus 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?
 
N

Nils Petter Vaskinn

On Tue, 29 Jul 2003 12:48:14 +0200
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
 
P

Pieter Droogendijk

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

Matthew Jakeman

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.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.identifier[0] != '\0')

what if allMenus 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.
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.


Thanks again.
Matt
 
D

Dan Pop

In said:
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.identifier) ;
while(allMenus.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.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
 
M

Matthew Jakeman

On Tue, 29 Jul 2003 12:48:14 +0200
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 != NULL)
{
lToken = strtok(fLines, " ") ;
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.identifier) ;
while(allMenus.identifier[0] != '\0')
{
printf("going through the loop again") ;
i++ ;
}
}

Thanks
Matt
 
M

Matthew Jakeman

On Tue, 29 Jul 2003 12:48:14 +0200

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 != NULL)
{
lToken = strtok(fLines, " ") ;
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.identifier) ;
while(allMenus.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!!
 
N

Nils Petter Vaskinn

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 != NULL)
{
lToken = strtok(fLines, " ") ;
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.identifier) ;
while(allMenus.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.identifier && allMenus.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
 
P

Pieter Droogendijk

On Tue, 29 Jul 2003 14:14:37 +0100
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.
 
P

Peter Slootweg

Al Bowers said:
Matthew said:
On Tue, 29 Jul 2003 13:31:40 +0100, Matthew Jakeman wrote:


On Tue, 29 Jul 2003 12:48:14 +0200


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 != NULL)
{
lToken = strtok(fLines, " ") ;
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.identifier) ;
while(allMenus.identifier[0] != '\0')
{
printf("going through the loop again") ;
i++ ;
}
}


--
Al Bowers
Tampa, Fl USA
mailto: (e-mail address removed) (remove the x)
http://www.geocities.com/abowers822/
 
M

Matthew Jakeman

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 = malloc(strlen(toArray)) ;
fLines = toArray ;
lToken = strtok(NULL, "\r\n") ;
i++ ;
}
else
{
lToken = strtok(NULL, "\r\n") ;
}
}
i = 0 ;
while(fLines != NULL)
{
lToken = strtok(fLines, " ") ;
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, "{") != 0)
{
printf("Error parsing menu config file after MENU %s\n", lToken) ;
exit(0) ;
}
else
{
i++ ;
}
while(strcmp(fLines, "}") != 0)
{
lToken = strtok(fLines, " ") ;
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.identifier) ;
while(allMenus.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
 
G

garagerules.fsnet.co.uk

Pieter Droogendijk said:
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 = malloc(strlen(toArray)) ;
fLines = toArray ;


No way, you did NOT just do that! First you make fLines point to a
just-malloc'd memory, then you point it to 'toArray'. What you probably want, is
fLines = malloc (strlen(toArray)+1);
fLines = strcpy (fLines, 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 != 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, " ") ;
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, "{") != 0)
{
printf("Error parsing menu config file after MENU %s\n", lToken) ;
exit(0) ;
}
else
{
i++ ;
}
while(strcmp(fLines, "}") != 0)
{
lToken = strtok(fLines, " ") ;
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.identifier) ;
while(allMenus.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.


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
 
P

Pieter Droogendijk

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 = malloc(strlen(toArray)) ;
fLines = toArray ;


No way, you did NOT just do that! First you make fLines point to a
just-malloc'd memory, then you point it to 'toArray'. What you probably want, is
fLines = malloc (strlen(toArray)+1);
fLines = strcpy (fLines, 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 != 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, " ") ;
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, "{") != 0)
{
printf("Error parsing menu config file after MENU %s\n", lToken) ;
exit(0) ;
}
else
{
i++ ;
}
while(strcmp(fLines, "}") != 0)
{
lToken = strtok(fLines, " ") ;
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.identifier) ;
while(allMenus.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.
 
C

Chris Dollin

garagerules.fsnet.co.uk said:
"Pieter Droogendijk" <[email protected]> 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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,951
Messages
2,570,113
Members
46,698
Latest member
alexxx

Latest Threads

Top