i am unable to find why following code is giving segmentation
fault.... way to produce seg fault: run the program... give input
12345678....enter any key except 'x'.... again give 12345678 as
input...then segmentation fault happens...
please somebody enlighten me...
#include <stdio.h>
#include<stdlib.h>
typedef struct
{
char project_id[40];
int project_ID;
} project_detail;
int
get_valid_int (char input[])
{
int count = 0;
while (input[count] != '\0' && count < 9)
{
if (input[count] >= '0' && input[count] <= '9')
count++;
else
return -1;
}
if (count != 8)
return -1;
else
return (atoi (input));
}
int
validate_projectid (int project_id)
{
FILE *fptr;
project_detail *temp;
int found = 0;
fptr = fopen ("PROJECT_DETAILS.DB", "r");
if (fptr != NULL)
{
fread (temp, sizeof (project_detail), 1, fptr);
while ((found == 0) && (!feof (fptr)))
{
if (temp->project_ID == project_id)
found = -1;
fread (temp, sizeof (project_detail), 1, fptr);
}
fclose (fptr);
}
return ((found >= 0) ? 1 : -1);
}
void
generation_of_project_details_add ()
{
project_detail proj_details;
char choice;
int search = 0;
FILE *f_ptr;
/*taking all the value from the keyboard */
do
{
/* Checking validity of Project detail ID */
do
{
printf ("\n Give the identification number of project\n");
scanf ("%s", proj_details.project_id);
fflush (stdin);
proj_details.project_ID = get_valid_int
(proj_details.project_id);
if (proj_details.project_ID == -1)
{
printf ("Invalid number. Pls enter again.\n");
continue;
}
search = validate_projectid (proj_details.project_ID);
/* Give error message if input is invalid */
if (proj_details.project_ID == -1 || search == -1)
printf
("\n The given input is invalid... Please try again...
\n");
}
while (proj_details.project_ID == -1 || search == -1); /* end
while
loop(inner) */
f_ptr = fopen ("PROJECT_DETAILS.DB", "a");
fwrite (&proj_details, sizeof (proj_details), 1, f_ptr);
fclose (f_ptr);
printf
("Do you want to input any other project detail. Press any key
to continue and press x to exit");
scanf (" %c", &choice);
}
while (choice != 'x');
}
int
main ()
{
generation_of_project_details_add ();
return 0;
} 3 7972
"Anks" <an******@mailcity.com> wrote in message i am unable to find why following code is giving segmentation fault.... way to produce seg fault: run the program... give input 12345678....enter any key except 'x'.... again give 12345678 as input...then segmentation fault happens...
please somebody enlighten me...
You will find that segmentation faults are very common in newly-written C
programs. This is because C makes it so easy to accidentally write to memory
that you don't own.
Generally, you need to run a debugger to find out on which line the error
occurred. Usually it is then pretty obvious what is wrong.
If you have no debugger, or the debugger doesn't help, you need to put in
diagnostic printf()s (printf("Here\n") etc) and comment out sections of
code, to home in on the error. #include <stdio.h> #include<stdlib.h>
typedef struct { char project_id[40]; int project_ID; } project_detail;
int get_valid_int (char input[]) { int count = 0;
while (input[count] != '\0' && count < 9) { if (input[count] >= '0' && input[count] <= '9') count++;
else return -1; } if (count != 8) return -1;
else return (atoi (input)); }
This function looks ok, provided input is a pointer to a valid address. int validate_projectid (int project_id) { FILE *fptr; project_detail *temp; int found = 0;
fptr = fopen ("PROJECT_DETAILS.DB", "r"); if (fptr != NULL) { fread (temp, sizeof (project_detail), 1, fptr);
This will cause an error. temp is an unitialised pointer pointing to who
knows where in memory, and you are writing data to it. while ((found == 0) && (!feof (fptr)))
This use of feof() probably isn't correct. feof() returns true if the last
attempt to read failed. { if (temp->project_ID == project_id) found = -1; fread (temp, sizeof (project_detail), 1, fptr);
ditto here } fclose (fptr); } return ((found >= 0) ? 1 : -1); }
void generation_of_project_details_add () { project_detail proj_details; char choice; int search = 0; FILE *f_ptr;
/*taking all the value from the keyboard */ do
{
/* Checking validity of Project detail ID */ do
{ printf ("\n Give the identification number of project\n"); scanf ("%s", proj_details.project_id);
This could also cause a segfault, if someone enters more characters than the
array can hold. fflush (stdin);
Flushing is for output streams only. proj_details.project_ID = get_valid_int (proj_details.project_id); if (proj_details.project_ID == -1)
{ printf ("Invalid number. Pls enter again.\n"); continue; } search = validate_projectid (proj_details.project_ID);
/* Give error message if input is invalid */ if (proj_details.project_ID == -1 || search == -1) printf ("\n The given input is invalid... Please try again... \n"); } while (proj_details.project_ID == -1 || search == -1); /* end while
loop(inner) */ f_ptr = fopen ("PROJECT_DETAILS.DB", "a"); fwrite (&proj_details, sizeof (proj_details), 1, f_ptr); fclose (f_ptr); printf ("Do you want to input any other project detail. Press any key to continue and press x to exit"); scanf (" %c", &choice); } while (choice != 'x'); }
int main () { generation_of_project_details_add (); return 0; } an******@mailcity.com (Anks) wrote: i am unable to find why following code is giving segmentation fault.... way to produce seg fault: run the program... give input 12345678....enter any key except 'x'.... again give 12345678 as input...then segmentation fault happens...
please somebody enlighten me...
<SNIP>int validate_projectid (int project_id) { FILE *fptr; project_detail *temp; int found = 0;
fptr = fopen ("PROJECT_DETAILS.DB", "r"); if (fptr != NULL) { fread (temp, sizeof (project_detail), 1, fptr);
Dang! Dang! Dang!
You failed to allocate some memory for temp to point to;
fread()ing to a non-existent buffer causes nasal demons, AKA
undefined behaviour.
IMHO you should write:
project_detail temp;
/* ^^^^^ */
[...]
fread ( &temp, sizeof temp, 1, fptr);
/* ^^^^^ */
[...]
if (temp.project_ID == project_id)
/* ^^^^^ */
[etc.]
<SNIP>
Two more hints:
- your sample program is way oversized; if you post code here
please make sure you cut it down to what is absolutely necessary
to exhibit the problem. Eventually this might result in finding
out what the problem /is/ by yourself... :)
- the overall program logic looks unnecessarily complex to me
Regards
Irrwahn
--
do not write: void main(...)
do not use gets()
do not cast the return value of malloc()
do not fflush( stdin )
read the c.l.c-faq: http://www.eskimo.com/~scs/C-faq/top.html
On Sat, 13 Sep 2003 17:22:20 UTC, an******@mailcity.com (Anks) wrote: i am unable to find why following code is giving segmentation fault.... way to produce seg fault: run the program... give input 12345678....enter any key except 'x'.... again give 12345678 as input...then segmentation fault happens...
int validate_projectid (int project_id) { FILE *fptr; project_detail *temp; int found = 0;
fptr = fopen ("PROJECT_DETAILS.DB", "r"); if (fptr != NULL) { fread (temp, sizeof (project_detail), 1, fptr);
temp is uninitialised! while ((found == 0) && (!feof (fptr))) { if (temp->project_ID == project_id) found = -1; fread (temp, sizeof (project_detail), 1, fptr); } fclose (fptr); } return ((found >= 0) ? 1 : -1); }
void generation_of_project_details_add () { project_detail proj_details; char choice; int search = 0; FILE *f_ptr;
/*taking all the value from the keyboard */ do
{
/* Checking validity of Project detail ID */ do
{ printf ("\n Give the identification number of project\n"); scanf ("%s", proj_details.project_id); fflush (stdin);
Undefined behavior as fflush() is only defind to work on output
streams.
--
Tschau/Bye
Herbert
eComStation 1.1 Deutsch Beta ist verügbar This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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:...
|
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...
|
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...
|
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...
|
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)?...
|
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!
...
|
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...
|
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...
|
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...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |