Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 499
  • Last Modified:

Linux atof problem

I have a C++ program that reads in a binary string, parses it into character arrays and converts some of them to doubles with atof.  It works fine on the Sun Sparcs on which the data is stored.  However, when I compile and run the program on Linux, the program bombs.  After debugging with gdb, I found that a string goes into atof as "1201" and comes out as 12019.  The Linux system nfs mounts the data disk from the same Sun.  I examined the "1201" in binary form, and it looks the same as on the Sun.

Thanks in advance.
0
jshirer
Asked:
jshirer
  • 2
1 Solution
 
dhmCommented:
Do you #include a header file that declares "double atof(const char *)"?  (Either <stdlib.h>, <fbm.h>, or some file that indirectly includes one of them.)  If not, you may be getting bitten by a return-type conversion bug.

Otherwise, check that the "1201" string is really '\0'-terminated.  If there's some other byte at the end of the string, it's possible that the Linux atof is attempting to continue conversion further than it should, while the Sun is stopping at the first non-numeric character.

(I just ran a test program on Linux here; I found that if you don't declare atof(), you get a bogus result, but if you do, you get the correct result -- even if there's a garbage character at the end of the string you convert.)
0
 
jshirerAuthor Commented:
Thanks!  There was another byte at the end of the string.  The string was created with the strncpy, not strcpy.  The strncpy on the sun put '/000' as the last byte.  Linux put '9', for whatever reason.  I just had to specify that the last byte = '\0', and it worked!  There are a lot of happy people here now.
0
 
dhmCommented:
It's unlikely that the strncpy put the "9" there; strncpy copies only as many bytes as you tell it to.  Probably, the buffer you were copying into was uninitialized (i.e. contained garbage), and the garbage on the Sun just happened to be '\0', while on Linux it was '9'.  Gotta watch those memcopy-type functions :-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now