The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 04-18-2008
DreamWarrior DreamWarrior is offline
Registered User
 

Join Date: Oct 2003
Posts: 69
C assumes (grrrrr, though a good compiler can warn you) that any function it does not see a prototype for prior to your calling it takes an int and returns an int. Since you did not include stdlib.h, it makes this very incorrect assumption for both atof and strtod. Your explicit cast to double even prevents the warning from popping out (explicit casts can be evil!).

It could be that on your platform, you are truncating/misaligning/generally messing up the return value for atof because C assumes that it is int. Due to this assumption, it is performing an (int) -> (double) conversion rather than the correct (no) conversion. Of course, going into the function it is also performing a (char *) -> (int) conversion, which is unusally (but not impossibly) a source of problems too.

In fact, I just recently had a guy come to me with an odd problem which turned out to be caused by a (void *) -> (int) conversion. He said he was passing a pointer to shared memory into a function and was getting "odd" results from this function. My immediate thought that was that pointers to shared memory on our 64 bit architecture machines likely have more resolution than an int (32 bit) can store (ie the upper half of the pointer value is not just zero'd out). I assumed he must not have had included the header and therefore all pointers passed into the function were being truncated to integer resolution. Hence, the real pointer value was never making it into the function and odd results were occuring. He included the header file, all worked well again, I saved the day, lol.
Reply With Quote