integer comparation (a == 0) - segmentation fault

integer comparation (a == 0) - segmentation fault

Post by Paulo dos Santo » Thu, 11 Jan 2001 20:48:10



I am with a problem that I don't know the cause.
I make three functions (main(), read_file(arg, arg), write_file(arg,
arg, arg)).
The main() function call the write_file(). The structure of this last
(function) is the following:

write_file(char *arg1, char *arg2)
FILE *Arq;
int a=0, b=0;
char *ab, *ac, *ad;

ab = malloc(10);
ac = malloc(10);
ad = malloc(10);

all right. next I verify if this vars allocated. next I open a file in
read mode - 'r' - (fopen) and do a verification if occurs errors on
this operation (to open). inside  a loop (the control is done by a
comparation strcmp(fgets(Arq), arg1)) I assign to var 'ab' the return
of the fgets(Arq) function.
I verify if is end-of-file (feof(Arq)). if true, is performed a
verification a=0. if the condition was satisfied the file is blank (no
records). if false (a!=0) is performed other verification b=0. if b
equal 0 the argument arg1 doesn't exists on the opened file.
case not be end-of-file, the var 'a' is incremented by 1 (a++) and the
following declaration is executed:
b = strcmp(fgets(Arq), arg1). the loop restart.

the problem is on the comparation of the vars 'a' and 'b'. when these
is compared with 0 (a==0 and b==0) the program aborts (Segmentation
fault. Core dumped). but when these is compared with values greater
than 0 (a>0 e b>0) the function normally continues.

what is the restriction (or problem) in this case?

note: I am using the GNU GCC compiler on a redhat linux 6 (2.2.14
kernel).

Thank you and excuse me for the spell errors.

Sent via Deja.com
http://www.deja.com/

 
 
 

integer comparation (a == 0) - segmentation fault

Post by Robert J. Hanse » Sun, 14 Jan 2001 11:14:13


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quote:>I am with a problem that I don't know the cause.

Unfortunately, it's hard to debug software without seeing the source
code.  Can you post the entire function which causes this segfault?
It'll make it a lot easier for the rest of us to look at.

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 6.5.8 for non-commercial use <http://www.pgp.com>

iQA/AwUBOl+yx5gOGKeCwjkrEQIVjQCcDMbx3cxA91Z5Di1LHmtnt0DkDfwAoIVd
wNNtIfjxw1p8zQ85/iWDiVL9
=FKvl
-----END PGP SIGNATURE-----

 
 
 

integer comparation (a == 0) - segmentation fault

Post by NiTrOgE » Mon, 15 Jan 2001 10:59:23


I'm new in C, but know C++ for a long time.

Try posting the full code, we'll find a way.

See yap,
Nitrogen

P.S.: Saudacoes brasileiras!

Sent via Deja.com
http://www.deja.com/

 
 
 

integer comparation (a == 0) - segmentation fault

Post by The Ghost In The Machi » Wed, 17 Jan 2001 12:35:39


In comp.os.linux.development.apps, Paulo dos Santos

 wrote
on Wed, 10 Jan 2001 11:48:10 GMT

Quote:>I am with a problem that I don't know the cause.
>I make three functions (main(), read_file(arg, arg), write_file(arg,
>arg, arg)).
>The main() function call the write_file(). The structure of this last
>(function) is the following:

>write_file(char *arg1, char *arg2)
>FILE *Arq;
>int a=0, b=0;
>char *ab, *ac, *ad;

>ab = malloc(10);
>ac = malloc(10);
>ad = malloc(10);

Without knowing what these are for, this looks rather peculiar.
What you're instructing is the allocation of 10 byte for each
of ab, ac, and ad.

Quote:

>all right. next I verify if this vars allocated. next I open a file in
>read mode - 'r' - (fopen)

Pedant point: "r".  (Mind you, gcc/g++ will warn you.)

Quote:>and do a verification if occurs errors on
>this operation (to open). inside  a loop (the control is done by a
>comparation strcmp(fgets(Arq), arg1)) I assign to var 'ab' the return
>of the fgets(Arq) function.

This is simply wrong.  fgets() returns a FILE *, and requires
3 arguments.  ('man fgets' for details on this function.)
fgets() will return either the third argument (the file pointer),
or NULL (indicating EOF or an I/O error).

strcmp(), by contrast, takes 2 arguments, both of which are
char * or const char * pointers.  ('man strcmp'.)

You either want fgets(ab, 10, Arq) (which reads a single
newline-terminated('\n') line from a file, maximum of 9 characters
(+ 1 for the terminating NUL character ('\0')), or maybe
fread(ab, 1 , 10, Arq), which reads in exactly 10 bytes at a time,
no more, no less, until end of file is hit.  Note that fread
will return an integer (the number of structures read -- in this
case, each "structure" is 1 byte in size), and will not terminate
the buffer with a NUL character; I wouldn't recommend using it
unless you know exactly what you're doing.  ('man fread'.)

You can then validate that fgets() worked (it returns NULL on
end of file or I/O error), then you can compare ab to arg1.
Something like this would probably work:

        while(fgets(ab, 10, Arq) != NULL)
        {
                if(strcmp(ab, arg1)==0)
                {
                        /* they're equal */
                }
        }

although it depends on what you really want to do.

Quote:>I verify if is end-of-file (feof(Arq)). if true, is performed a
>verification a=0. if the condition was satisfied the file is blank (no
>records). if false (a!=0) is performed other verification b=0. if b
>equal 0 the argument arg1 doesn't exists on the opened file.
>case not be end-of-file, the var 'a' is incremented by 1 (a++) and the
>following declaration is executed:
>b = strcmp(fgets(Arq), arg1). the loop restart.

>the problem is on the comparation of the vars 'a' and 'b'. when these
>is compared with 0 (a==0 and b==0) the program aborts (Segmentation
>fault. Core dumped). but when these is compared with values greater
>than 0 (a>0 e b>0) the function normally continues.

>what is the restriction (or problem) in this case?

Well, there are a fair number of usage errors of fgets() and
strcmp() in the code snippets you have given us; until those
are fixed, you won't get very far. :-)

Quote:

>note: I am using the GNU GCC compiler on a redhat linux 6 (2.2.14
>kernel).

>Thank you and excuse me for the spell errors.

>Sent via Deja.com
>http://www.deja.com/

--

EAC code #191       4d:08h:48m actually running Linux.
                    Are you still here?
 
 
 

1. Compiling *** VIM 5.3 *** Segmentation Fault..what is Seg-Fault..MEM Bounds?

Compiled fine........upgraded from 4.6  REdhat 5.0

got the src and rt of the autors VIM site via ftp.vim.org

changed the first requested by auther line in ".vimrc" file to not
read "version 4.00" and it worked once.....Gee

Now I am using it on everything ...DOS Win95 even NT40 !!!!

I know go back to the early version but still.....
what or why ...

"Segmentation Fault...Core dump"
thanks in advance.....Matt

2. Online Log Book

3. Page Faults/Segmentation Faults??

4. SOCK_PACKET ???

5. Fatal Trap 18: Integer divide fault while in Kernel mode

6. SATAN. Where can I find it?

7. 4.7 Installation hangs with Integer Divide Fault

8. /etc/login.access

9. "Segmentation fault( core dumped ) "<--- sentence is driving me mad!!!!!

10. segmentation fault ?

11. Get "Segmentation fault (core dumped)" but no core file found

12. Segmentation Faults and Bus Errors

13. Dosemu0.52 : Segmentation fault?