Re:Found a bug in the GCC system (not deep, complex or new, ...
by RiderOfGiraffes on Friday May 21, 2010 @01:05PM
(#1367965)
Right, I believe I now understand more of what's happening.
Prior to C99 there was no strtof. Some people may have rolled their own, but it wasn't provided, you used strtod and cast to float if that's what you wanted.
Then C99 included strtof, so the code (and hence symbol) became available in the libraries. However, to avoid clashing with legacy code in which someone used their own version, the prototype is only declared if the compiler is in C99 mode.
That means that if you're not in C99 mode, you use the symbol strtof, and you don't declare the function for yourself, the compiler successfully finds the symbol in the library and links to the C99 code, but hasn't declared the prototype, so you don't get what you think you get.
Re:Found a bug in the GCC system (not deep, complex or new, ...
by pmiller2 on Saturday May 22, 2010 @05:27AM
(#1370071)
I always use "-W -Wall -ansi -pedantic". Substitute -std=c99 for -ansi if you swing that way. I then run my code through a couple of other static checkers just to see what comes up.
This frequently results in me finding some subtle bug which otherwise could have taken hours of head scratching or single-stepping in gdb to locate.
Re:Found a bug in the GCC system (not deep, complex or new, ...
by astrange on Saturday May 22, 2010 @10:31PM
(#1371600)
"-Werror=implicit" would be more useful here. -Wall won't prevent that from compiling, but there's hardly any situation where you'd want it to compile.
Re:Found a bug in the GCC system (not deep, complex or new, ...
by jmillikin on Friday May 21, 2010 @11:58AM
(#1367732)
Misleading headline -- GCC is behaving according to spec. It would have a bug if the output had been 5.7 in both cases.
Arguably, allowing procedures to be called without prototypes is a bug in the C spec, but I doubt it'll be removed any time soon simply due to sheer volume of extant legacy code.
Re:Found a bug in the GCC system (not deep, complex or new, ...
by jey on Friday May 21, 2010 @12:27PM
(#1367817)
Scroll down in your manpage to the "CONFORMS TO" section. You'll see that strtof was only introduced in C99. This means that stdlib.h tests some macro to determine whether it's in C99 mode before declaring the prototype for strtof.
Re:Found a bug in the GCC system (not deep, complex or new, ...
by RiderOfGiraffes on Friday May 21, 2010 @01:10PM
(#1367972)
To be fair, recent versions probably do. For reasons I won't (or can't) go into, I have a tendency to be using slightly older systems. That's why I'm more interested in finding out how things can be traced, rather than worrying about this specific mis-match between my expectations and the system's behavior.
Re:Found a bug in the GCC system (not deep, complex or new, ...
by tedunangst on Friday May 21, 2010 @12:39PM
(#1367859)
Besides this not being a GCC bug, the explanation is also wrong.
"Apparently GCC has a built-in version of strtof, which it compiles into your code if you're not using c99."
No, your libc has a function called strtof which the compiled program links to. Just like any and every other external reference and completely independent of where the prototype comes from.
Re:Found a bug in the GCC system (not deep, complex or new, ...
by kmak on Friday May 21, 2010 @11:16AM
(#1367569)
I've been bit by this before -- in my case it was math.h and hypot. It was quite frustrating to debug something that I assumed would be correct, so did not look at.
Prior to C99 there was no strtof. Some people may have rolled their own, but it wasn't provided, you used strtod and cast to float if that's what you wanted.
Then C99 included strtof, so the code (and hence symbol) became available in the libraries. However, to avoid clashing with legacy code in which someone used their own version, the prototype is only declared if the compiler is in C99 mode.
That means that if you're not in C99 mode, you use the symbol strtof, and you don't declare the function for yourself, the compiler successfully finds the symbol in the library and links to the C99 code, but hasn't declared the prototype, so you don't get what you think you get.
And by default you don't get warned.
To repeat the moral: use -Wall.
"The moral? Always use -Wall"
This frequently results in me finding some subtle bug which otherwise could have taken hours of head scratching or single-stepping in gdb to locate.
C is a somewhat weak foundation for static program analysis but that doesn't mean we can't try to make the most out of it.
Arguably, allowing procedures to be called without prototypes is a bug in the C spec, but I doubt it'll be removed any time soon simply due to sheer volume of extant legacy code.
"Apparently GCC has a built-in version of strtof, which it compiles into your code if you're not using c99."
No, your libc has a function called strtof which the compiled program links to. Just like any and every other external reference and completely independent of where the prototype comes from.
Summary in a comment elsewhere in this submission.