[comp.lang.c] The bug in Turbo C 2.0

chii@ee.su.oz.au (Liang Chii ) (03/15/90)

Hi, net reader,

I found a bug in Turbo C, please reconfirm if it is truely a bug.
======= cut here =======
#include <stdio.h>
#include <alloc.h>

main() {
float far *temp, fix;
int i;

temp = farmalloc(40);
for(i = 0; i< 5; i++)
scanf("%f", temp+i);
}
===== end here ======

The above code does not work but with an error message :
"floating point formats not linked"

But, if we put another statement at the end of the program :

scanf("%f", &fix);

Now, re-compiler and link the code.  It works ok.
It is very obvious that the code "scanf("%f", &fix);" should not
affect the about program like this.  I believe there is a bug
between farmalloc(farcalloc) and scanf("%f",...).

That is what I found.

==========================================================================
YungChii Liang, Power Electronics and Drives Group 
                School of Electrical Engg., University of Sydney
                NSW 2006, Australia
                TEL: 02 692 2337  FAX: 02 692 3847
e-mail : chii@ee.su.oz.au                       oo 
         chii@facet.ee.su.oz              ^-^  o   The man from Formosa 
         chii@extro.ucc.su.oz.au         (o o)
                                           ~       

chii@ee.su.oz.au (Liang Chii ) (03/15/90)

Hi, net reader,

I forgot to mention in the previous mail "The bug in Turbo C 2.0"
that, even the pointer is near and run in small memory model,
it is still a bug.

This tells us that, malloc() and scanf("%f",...)  are not able
to work together unless we do have another scanf("%f",..) to read
a plain float variable which is independent to malloc().

That is what I found again.
By the way, any one knows the e-mail address to Borland ?
Thanks.

==========================================================================
YungChii Liang, Power Electronics and Drives Group 
                School of Electrical Engg., University of Sydney
                NSW 2006, Australia
                TEL: 02 692 2337  FAX: 02 692 3847
e-mail : chii@ee.su.oz.au                       oo 
         chii@facet.ee.su.oz              ^-^  o   The man from Formosa 
         chii@extro.ucc.su.oz.au         (o o)
                                           ~       

randall@uvaarpa.virginia.edu (Randall Atkinson) (03/15/90)

I don't see any proof of compiler error in the posting I'm
following up to, but I do have Borland's e-mail address.

Please send them mail if you have questions about their
products.  The e-mail Tech Support folks are really very
good and you can get your question answered much more
effectively than by posting news that thousands of folks
will have to wade through.

Borland Tech Support:
  Larry Kraft  <76703.764@compuserve.com>

(Yes that is a numeric address not an article ID :-)

grimlok@hubcap.clemson.edu (Mike Percy) (03/16/90)

From article <1990Mar14.174620.11101@metro.ucc.su.OZ.AU>, by chii@ee.su.oz.au (Liang Chii			):
> Hi, net reader,
> 
> I found a bug in Turbo C, please reconfirm if it is truely a bug.
> ======= cut here =======
> #include <stdio.h>
> #include <alloc.h>
> 
> main() {
> float far *temp, fix;
> int i;
> 
> temp = farmalloc(40);
> for(i = 0; i< 5; i++)
> scanf("%f", temp+i);
> }
> ===== end here ======
> 
> The above code does not work but with an error message :
> "floating point formats not linked"
> 
> But, if we put another statement at the end of the program :
> 
> scanf("%f", &fix);
> 
> Now, re-compiler and link the code.  It works ok.
> It is very obvious that the code "scanf("%f", &fix);" should not
> affect the about program like this.  I believe there is a bug
> between farmalloc(farcalloc) and scanf("%f",...).
> 
 
This happens frequently with TC.  In my experience it is not what I would
exactly a bug, more like the user not knowing what the compiler is doing
and why it is doing it.  TurboC uses a floating point emulation library
if you do not have a co-processor.  Printf() drags this in even if you
do not do floating operations (unless you specify -f-).  If you do any
operations on a float  or double value, you get this brought in.
Scanf() does not drag the floating point stuff with it, because scanf
simply works on addresses and strings of characters (i.e. no float
operations), and puts its results in the memory you tell it to.  Its up to
the programmer to properly size and treat that memory!

The reason your 
code didn't work properly in the first case was because you haven't done
any operations on (technically) floats or doubles.  What you did was 
allocate a pointer to something that is sized and can safely be treated
as a float.  You then pointed this at 40 bytes of memory, which you want 
to treat as a number of floats to be read by scanf, but which you could just have easily wanted to be a bunch of ints or structs.  The fix you propose     
works not beacuse of some glitch in scanf, but because now you have performed 
an operation (address of) something which the compiler _knows_ is a float,
and not just some memory which happens to be treated like a float. 
A fix which is use more often is to add a line in main() like
(void) log(1.0);

Your code now includes something the compiler recognizes as indicative of
requiring the floating point libraries, and wham, bam, you're set.

Good luck.

weeks@ssbell.IMD.Sterling.COM (John Weeks) (03/17/90)

In article <1990Mar14.174620.11101@metro.ucc.su.OZ.AU! chii@ee.su.oz.au (Liang Chii			) writes:
!I found a bug in Turbo C, please reconfirm if it is truely a bug.
!======= cut here =======
!#include <stdio.h!
!#include <alloc.h!
!
!main() {
!float far *temp, fix;
!int i;
!
!temp = farmalloc(40);
!for(i = 0; i< 5; i++)
!scanf("%f", temp+i);
!}
!===== end here ======
!
!The above code does not work but with an error message :
!"floating point formats not linked"

Its a fact that the Turbo C linker doesn't link in the floating
point routines if it doesn't think they are needed.  So in some
circumstances you have to force them to be linked in by making
an appropriate reference.  I think just declaring a dummy function
call will work:   void dummy(float a);

(Its a bug if they want to fix it, a feature if they don't. :-)

                          -jw-
-- 

John Weeks                                    Phone:  (402) 291-8300
Sterling Software FSG/IMD        e-mail: uunet!ssbell!weeks
1404 Ft. Crook Rd. South         e-mail: weeks@ssbell.IMD.Sterling.COM