[comp.lang.c] Memor fault, Core dump

nnj20229@uxa.cso.uiuc.edu (Nesha Nicole Jones) (05/21/91)

I am having a small problem debugging a program of mine. I get a memory
fault after a fprintf statement.  I was trying to allow the user to input 
more than one line of information at a prompt.  The program prints the
printf statement and then then does the fprintf and then bombs with a 
memory fault.  I know memory faults can be caused by a lot of things
but I was wondering if you could point me in the correct direction for
debugging it.  


also, I read in two values, a date and a time.  I strcat the date and time
together and the value for date is somehow changed to the 5th char in time
ex.  09:45   date= 5


Any suggtestions about why this happens would also be helpfull.

Thanks,
NNJ 

gordon@osiris.cso.uiuc.edu (John Gordon) (05/22/91)

	If you expect people to spend time debugging someone else's code, you
could at least provide the code, not just an explanation of what it does.

But anyway: Whenever you get a memory fault error, it means that your
program was trying to access memory that it did not own.  This almost always
means that you are accessing an element of an array that is out-of-bounds,
or a pointer is pointing to the wrong place.  Make sure that the array you
are strcat()'ing into is big enough.  Make sure that the arguments to
fprintf() are in the right order.


---
John Gordon
Internet: gordon@osiris.cso.uiuc.edu        #include <disclaimer.h>
          gordon@cerl.cecer.army.mil       #include <clever_saying.h>

mostek@motcid.UUCP (Frank B. Mostek) (05/29/91)

nnj20229@uxa.cso.uiuc.edu (Nesha Nicole Jones) writes:

>also, I read in two values, a date and a time.  I strcat the date and time
>together and the value for date is somehow changed to the 5th char in time
>ex.  09:45   date= 5

It would be easier if you included the piece of code.  The strcat
function sometimes causes problems.  It assumes the destination address
points to an area of memory that is big enough to hold the resulting
concatenated data.  Make sure that your destination is big enough to
hold both the date and time. (Assuming that is all that you are ever
concatenating.)

Also, make sure that your parameters are correct to the print functions.

Hope this helps.
-- 
Frank Mostek			uunet!motcid!amethyst!mostek
(708)632-6965			mostek@amethyst.mot.com

wolfram@cip-s01.informatik.rwth-aachen.de (Wolfram Roesler) (06/05/91)

nnj20229@uxa.cso.uiuc.edu (Nesha Nicole Jones) writes:

>I am having a small problem debugging a program of mine. I get a memory
>fault after a fprintf statement.

First idea: did you pass the FILE* as the first arg to fprintf? This is
usually the reason for me...

>I was trying to allow the user to input 
>more than one line of information at a prompt. 

Ha! Just imagine the user inputs a line like: "hello you %s", then
you will have `fprintf(fp,"hello you %s");'. fprintf will then encounter
the %s, take something from its vararg list, assume it's a pointer and
BOUMMMMM! To print something the user has entered (say, in a string named St),
NEVER say `fprintf(fp,St)' but use `fprintf(fp,"%s",St)' or even better
`fputs(St,fp)'.
You should use [sf]printf only if you actually want to make use of the format
elements introduced by '%'. The same problem is `sprintf(s1,s2)', use
strcpy instead.

>also, I read in two values, a date and a time.  I strcat the date and time
>together and the value for date is somehow changed to the 5th char in time
>ex.  09:45   date= 5

Please explain that more precisely.

>Any suggtestions about why this happens would also be helpfull.

I hope so.

BCingU
\/\/olfram