473,323 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

segmentation fault

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
15 7087
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
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
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
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
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
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
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
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
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
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

"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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
3
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers:...
16
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.