Re: Help wanted on some source codes



In article <1133311547.679305.247520@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Becker <webmaster@xxxxxxxxx> wrote:
>1.===========================================
>/* a.c */
>int x;
>int y;
>
>void main()
>{
> f();
> printf("%x %x\n", x, y);
>}

%x expects an unsigned int, not an int.

>/* b.c */
>double x;
>
>void f()
>{
> x = -0.0;
>}

You never initialize y at all, and you initialize x to the wrong
type. double might or might not be the same size as int; it is not
uncommon for it to be longer, but that is not certain. If it is longer
then you -might- end up overwritting y, but the relative order of x and
y are not fixed by the standard, and double might be larger than two
ints together so you might be scribbling on a random portion of memory.

If you happen to be using IEEE754 floating point, then -0.0 is
considered to be different than 0.0 . Even on systems that it is not,
the floating point representation of 0 is not certain to be all binary
0's (though -some- floating point standards require that all-binary 0's
must be treated as floating point 0.)

Your programs 2 thru 4 are just variations on exactly the same themes
with various different values initializing the memory that might or might
not be scribbled over.


>5.===========================================

>void f()
>{
> x = -0;
>}

-0 is an integer constant, and it is the same as 0 on all but the
rather-uncommon signed-magnitude machines (which are allowed for by
the standard.) -0 as an integer is thus the same as 0 as an integer.
That integer is then cast to the double that you have declared x to
be in this file. On some systems that is different than the floating
point -0.0 .
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
.



Relevant Pages