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

Why segmentation fault

P: n/a
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;
}
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"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;
}

Nov 13 '05 #2

P: n/a
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
Nov 13 '05 #3

P: n/a
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
Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.