Random C issue, was (Re: [vorbis-dev] getting playback length from ogg vorbis file)

Kenneth C. Arnold kcarnold at arnoldnet.net
Fri Jul 11 20:33:22 PDT 2003



Jack Angel wrote:

>This doesn't work because <math.h> isn't included, and floor definition is picked up from somewhere else. I don't know from where, but check this:
>
>~# cat test.c
>//#include <math.h>
>
>int main()
>{
>  double time = 800.510;
>  printf("%f %f\n", time, floor(time));
>}
>~# ./test
>800.510000 80298907385280950682755444182387957006805762791233118433533549849731864380320828326878948200747786192971450917286372883160777116601003408797083834721392632613345848270168843448871733524979042229249230391219772018492598287395874874133804539689308263302039462290421043577395770761936896.000000
>

Compile with -Wall and you'll see why:

test.c:6: warning: implicit declaration of function `printf'
test.c:6: warning: implicit declaration of function `floor'
test.c:6: warning: double format, different type arg (arg 3)
test.c:7: warning: control reaches end of non-void function

gcc sees no prototype for "floor" and assumes that it is:

int floor(int)

(which is the "implicit declaration") because some C standard said that 
"int" is default. Not only are "int" and "double" different sizes (so 
you just corrupted your stack; "main" pushed 8 byes and libc popped 4), 
but the first 4 byes of a "double" are absolutely meaningless when 
interpreted as an "int".

This is, of couse, why compiler warnings are actually a good thing. 
CFLAGS += "-Wall".

Ken

<p>--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'vorbis-dev-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the Vorbis-dev mailing list