Hi Pitcher ,
It's because we are typecasting i's address into a double* and then passing
it to the incre() function. I know there is some issue with the typecasting
itself.Passing like this is illegal ...is it???.....and my apologies for the
silly abbrevations.
Never mind the type casting being illega.
Have you considerd that the type double and the type int do not even have the
same size? (They might, but often they do not. A common size for int is 32
bits nowadays, and a double is often 64 bits.)
What do you think happens when you pass a pointer to 32 bytes of storage
to a function which treats that as 64 bytes of storage?
Of course, that function stomps on memory outside of the object.
The code could well crash. What if incme overwrites the return address on the
stack, so that when it tries to return back to main, it jumps into nowhere
land, resulting in an access violation or illegal instruction trap or something
of that sort?
Listen, you better start replacing "I feel" with "I think", or find another
hobby.
Then, have you considered byte order issues? Suppose that the previous issue is
not a problem. Although incme stomps over memory beyond the boundaries of i,
let's suppose there is no problem because, let's say that that memory is just
some unused padding space or whatever. There is still the problem that on
some machines, values are laid out with the least significant bits at the base
address. On other machines, values are placed with the most significant bits
at the base address. This means that the int object i, if it is smaller than
double (like 32 bits versus 64), could correspond to the upper half of double,
where the sign and exponent are, or it could correspond to the lower half,
where there are mantissa bits.
Then there is the issue that incrementing a floating-point value by 1.0 is
completely different from incrementing an integer by 1.
Consider that if you add 1.0 to some really huge number like 1.0E+256,
it doesn't change at all, because 1.0 is too small in comparison.
However, if you add 1.0 to a really small number (close to zero) like
1.0E-256, the result is just 1.0 because the really small number
vanishes next to 1.0.
Which bits of the floating-point value are affected when 1.0 is added
to it (if any) and in what ways depends on the value. Adding 1.0
might change the exponent field, or it might not. It could change
the most significant bits of the mantissa, or the least significant
bits, or none at all.
Adding 1 to a postive integer is (in the absence of overflow) a purely
binary operation. The least significant bit increments, and if that
overflows, the next bit increments, and so on.
The two operations are not related at all, and so if you perform a floating
point increment on an integer or vice versa, you can get some very strange
results.