public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* B19: gcc/libc bug with call to atof()   (Win95)
@ 1998-10-27  6:20 Martin Hansen
  1998-10-28  1:33 ` Andres Heinloo
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Martin Hansen @ 1998-10-27  6:20 UTC (permalink / raw)
  To: gnu-win32

Dear list,

in my version of gnu-win32 I encounter the following bug with
gcc/libc.  
I have the gnu-win32 version B19 running under Win95 (4.00.950 B).
Within the gnu-win32 distribution I have the gcc version
% gcc -v
Reading specs from C:\CYGNUS\B19\H-I386~1\lib\gcc-lib\i386-cygwin32\2.7-B19\specs
gcc driver version 2.7-B19 executing gcc version 2.7-97r2aBeta



The following small program produces the bug:

/* test_atof.c */

#include <stdio.h>

main(int argc, char **argv)
{
   while (--argc)
   {
      printf("\narg %d is %s\n", argc, argv[argc]);
      printf("arg %d : value as float %f\n", argc, atof(argv[argc]));
      printf("arg %d : value as   int %d\n", argc, atoi(argv[argc]));
   }
}

/* End of file */




Compile the program by
    % gcc test_atof.c
The program compiles and I don't get any error about the missing line
"#include <stdlib.h>", where atoi() and atof() are declared.  (This is
the bug, part1)
 
Try the program with
    % test_atof  3.3 4.7
The result is

arg 2 is 4.7
arg 2 : value as float 0.000000
arg 2 : value as   int 4

arg 1 is 3.3
arg 1 : value as float 0.000000
arg 1 : value as   int 3

which is wrong for the float value but right for the int value.  (This
is the bug, part2).  First when you add the line '#include <stdlib.h>'
to the program and recompile it, you get the desired behaviour.
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: B19: gcc/libc bug with call to atof()   (Win95)
  1998-10-27  6:20 B19: gcc/libc bug with call to atof() (Win95) Martin Hansen
@ 1998-10-28  1:33 ` Andres Heinloo
  1998-10-28  6:00 ` Stephen Vance
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Andres Heinloo @ 1998-10-28  1:33 UTC (permalink / raw)
  To: Martin Hansen; +Cc: gnu-win32

On Tue, 27 Oct 1998, Martin Hansen wrote:

> Compile the program by
>     % gcc test_atof.c
> The program compiles and I don't get any error about the missing line
> "#include <stdlib.h>", where atoi() and atof() are declared.  (This is
> the bug, part1)

Not really. How about 'gcc -Wall -Wstrict-prototypes'?


Andres.


-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: B19: gcc/libc bug with call to atof()   (Win95)
  1998-10-27  6:20 B19: gcc/libc bug with call to atof() (Win95) Martin Hansen
  1998-10-28  1:33 ` Andres Heinloo
@ 1998-10-28  6:00 ` Stephen Vance
  1998-10-28  8:50 ` Mumit Khan
  1998-10-28 15:58 ` Peter A. Vogel
  3 siblings, 0 replies; 5+ messages in thread
From: Stephen Vance @ 1998-10-28  6:00 UTC (permalink / raw)
  To: Martin Hansen; +Cc: gnu-win32

Martin Hansen wrote:

> which is wrong for the float value but right for the int value.  (This
> is the bug, part2).  First when you add the line '#include <stdlib.h>'
> to the program and recompile it, you get the desired behaviour.

This is your clue.  This is not a bug.  C does not have "built-in" functions
(excepting keywords with function syntax like sizeof), only functions defined by the
Standard Library.  The function atof is prototyped in the header stdlib.h.  Without
a prototype, floats are promoted to doubles.  A good C book can explain the details
well.

What you are probably seeing is the float returned from atof() promoted to a double
and its first sizeof(float) bytes are being pulled off the stack by the stdarg
mechanisms used to implement printf().  The first four bytes of a double containing
the small value you are trying to print will be all 0s.  Thus, it prints 0s.

--
Stephen Vance                           |  http://www.deneb.com
Deneb Robotics, Inc.                    |  mailto:vance@deneb.com
5500 New King Street                    |  Phone: (248) 267-9696
Troy, MI 48098-2615                     |  Fax:   (248) 267-8585

What is done well is done quickly enough. -Augustus Caesar



-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: B19: gcc/libc bug with call to atof()   (Win95)
  1998-10-27  6:20 B19: gcc/libc bug with call to atof() (Win95) Martin Hansen
  1998-10-28  1:33 ` Andres Heinloo
  1998-10-28  6:00 ` Stephen Vance
@ 1998-10-28  8:50 ` Mumit Khan
  1998-10-28 15:58 ` Peter A. Vogel
  3 siblings, 0 replies; 5+ messages in thread
From: Mumit Khan @ 1998-10-28  8:50 UTC (permalink / raw)
  To: Martin Hansen; +Cc: gnu-win32

On Tue, 27 Oct 1998, Martin Hansen wrote:

> The following small program produces the bug:

[ ... ]

> The program compiles and I don't get any error about the missing line
> "#include <stdlib.h>", where atoi() and atof() are declared.  (This is
> the bug, part1)

Sorry, but your code is buggy. You *must* prototype atof before using 
it, otherwise the default return type in C is "int" and obviously you 
get garbage in this case. Either include <stdlib.h> or declare it 
yourself.

  $ gcc -c -Wall file.c

is a good check before blaming the compiler ...

Also, you probably should upgrade your compiler tools to egcs. See
Cygnus home page for link back to mine.

Regards,
Mumit


-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: B19: gcc/libc bug with call to atof()   (Win95)
  1998-10-27  6:20 B19: gcc/libc bug with call to atof() (Win95) Martin Hansen
                   ` (2 preceding siblings ...)
  1998-10-28  8:50 ` Mumit Khan
@ 1998-10-28 15:58 ` Peter A. Vogel
  3 siblings, 0 replies; 5+ messages in thread
From: Peter A. Vogel @ 1998-10-28 15:58 UTC (permalink / raw)
  To: Martin Hansen, gnu-win32

gcc is a real C compiler when it compiles C, C allows you to use
undeclared functions, assuming that all undeclared functions are
"extern int".  Since a to f returns a double, not a float, I would
assume that there is a call setup/teardown problem that occurs due
to the incorrect assumption about the return type of the function.

To quote from K&R 2nd edition, page 72:

"...But if (as is more likely) atof were compiled seperately, the 
mismatch would not be detected atof() would return a double that
main() would treat as an int, and meaningless answers would result." 

None of this is a "bug" except in your code.

-Peter

Peter A. Vogel
Manager, SW Configuration Management
Chromatic Research, Inc.  
http://www.chromatic.com

> -----Original Message-----
> From: owner-gnu-win32@cygnus.com [ mailto:owner-gnu-win32@cygnus.com]On
> Behalf Of Martin Hansen
> Sent: Tuesday, October 27, 1998 4:11 AM
> To: gnu-win32@cygnus.com
> Subject: B19: gcc/libc bug with call to atof() (Win95)
> 
> 
> Dear list,
> 
> in my version of gnu-win32 I encounter the following bug with
> gcc/libc.  
> I have the gnu-win32 version B19 running under Win95 (4.00.950 B).
> Within the gnu-win32 distribution I have the gcc version
> % gcc -v
> Reading specs from 
> C:\CYGNUS\B19\H-I386~1\lib\gcc-lib\i386-cygwin32\2.7-B19\specs
> gcc driver version 2.7-B19 executing gcc version 2.7-97r2aBeta
> 
> 
> 
> The following small program produces the bug:
> 
> /* test_atof.c */
> 
> #include <stdio.h>
> 
> main(int argc, char **argv)
> {
>    while (--argc)
>    {
>       printf("\narg %d is %s\n", argc, argv[argc]);
>       printf("arg %d : value as float %f\n", argc, atof(argv[argc]));
>       printf("arg %d : value as   int %d\n", argc, atoi(argv[argc]));
>    }
> }
> 
> /* End of file */
> 
> 
> 
> 
> Compile the program by
>     % gcc test_atof.c
> The program compiles and I don't get any error about the missing line
> "#include <stdlib.h>", where atoi() and atof() are declared.  (This is
> the bug, part1)
>  
> Try the program with
>     % test_atof  3.3 4.7
> The result is
> 
> arg 2 is 4.7
> arg 2 : value as float 0.000000
> arg 2 : value as   int 4
> 
> arg 1 is 3.3
> arg 1 : value as float 0.000000
> arg 1 : value as   int 3
> 
> which is wrong for the float value but right for the int value.  (This
> is the bug, part2).  First when you add the line '#include <stdlib.h>'
> to the program and recompile it, you get the desired behaviour.
> -
> For help on using this list (especially unsubscribing), send a message to
> "gnu-win32-request@cygnus.com" with one line of text: "help".
> 
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~1998-10-28 15:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-10-27  6:20 B19: gcc/libc bug with call to atof() (Win95) Martin Hansen
1998-10-28  1:33 ` Andres Heinloo
1998-10-28  6:00 ` Stephen Vance
1998-10-28  8:50 ` Mumit Khan
1998-10-28 15:58 ` Peter A. Vogel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).