473,756 Members | 3,499 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("identif ier %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 7244
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("identif ier %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,"a dceoX$_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);r eturn!s?10:10+* k;}
Nov 13 '05 #2
On Tue, 29 Jul 2003 12:48:14 +0200
Pieter Droogendijk <gi*@binky.home unix.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("identif ier %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,"a dceoX$_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);r eturn!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("identif ier %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.home unix.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.home unix.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,"a dceoX$_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);r eturn!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.home unix.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(lToke n, "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("identif ier %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.home unix.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(lToke n, "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("identif ier %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.uk> 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("identif ier %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 "insignific ant" 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(lToke n, "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("identif ier %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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6808
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 works fine and do all what I need. But at the end of the execution, I can read "Segmentation Fault". The segmentation fault appear at the end of my script execution,
3
1939
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: /var/www/cgi-bin/script.cgi when i debug the program i get Segmentation fault gdb ./script.cgi
16
8994
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 who don't speak french arbre means tree.
3
11442
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 () from /lib/tls/libc.so.6 It's really strange; I just call malloc() like "tmp=malloc(size);" the system gives me Segmentation fault I want to write a code to do like a dynamic array, and the code is as
5
2997
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 understood that a segmentation fault can occur whenever I declare a pointer and I leave it un-initialized. So I thought the problem here is with the (const char *)s in the stuct flightData (please note that I get the same fault declaring as char * the...
18
26118
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)? Thanks.
27
3361
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! #include <stdlib.h> #include <stdio.h> typedef struct _node_t {
7
5879
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 testcases we experiance Segmentation fault from the python libraries. If i know how to handle the exception for Segmentation fault , it will help me complete the run on any testcase , even if i experiance Seg Fault due to any one or many functions in...
3
5187
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 from 10g release2 PHP is configured with the following parameters './configure' '--prefix=/opt/oracle/php' '--with-apxs=/opt/oracle/apache/bin/apxs' '--with-config-file-path=/opt/oracle/apache/conf' '--enable-safe-mode' '--enable-session'...
6
5043
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 gcc. The only difference is that in first I only call "main" and in second call
0
9273
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7244
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5141
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5303
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.