Re: double casts

From: Dan Pop (Dan.Pop_at_cern.ch)
Date: 11/05/03


Date: 5 Nov 2003 16:56:42 GMT

In <bob3ic$nb4$2@oravannahka.helsinki.fi> Joona I Palaste <palaste@cc.helsinki.fi> writes:

>Dan Pop <Dan.Pop@cern.ch> scribbled the following:
>> In <boalcr$eg7$1@oravannahka.helsinki.fi> Joona I Palaste <palaste@cc.helsinki.fi> writes:
>>>And even if that's true, there's no guarantee the host CPU returns
>>>scalars and pointers in the same way. It could use different registers,
>>>for instance, in which case p2 will be pure garbage.
>
>> Huh? I can see no function call in the code. What am I missing?
>
>You're missing the fact that I can sometimes fumble up in my
>terminology. By "return" I meant something like "store". Is a cast to
>an (int *) allowed to read from a different register than a cast to a
>(long)?

Now, you're severely confused: the evaluation of an expression is defined
in terms of values, it is immaterial where these values are stored, as
long as all of them are obtained via legit means. It is compiler's job
to do the right thing and it has all the necessary information for that.

Such things can happen only when misdeclared functions are involved.
Consider the following *complete* program (on a C89 implementation):

    int main()
    {
        return sin(0.0);
    }

In the absence of an explicit declaration, sin() is implicitly declared
as returning int. Therefore, after generating the function call, the
compiler will expect the return value to be in whatever place a function
returning int will put its return value. But the sin() function doesn't
"know" that and it will put its return value in whatever place a function
returning double is supposed to put its return value. The final result
being that return will use an indeterminate value (but undefined behaviour
has already been invoked by the time sin() was called.

Even worse things can happen if the stack is used for passing back the
return value, because the caller generates a certain stack layout, while
the callee expects another, possibly corrupting stack data containing the
callers local variables or its return address.

Dan

-- 
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de


Relevant Pages

  • Re: threads question
    ... > either a bug or I am missing a vital piece of information somewhere. ... > this works perfectly because I moved MGPMrUpgrade into ... > through the stack to a function in a shared library isn't being handled ... Am I missing something obvious? ...
    (freebsd-hackers)
  • Re: sin x / x tends to 1...
    ... > You might note that there was a factor of 1/2 missing in my ... > definition of "aradian", which was cancelled by the missing ... sin x, that we can call psin x, the limit is obvious. ...
    (sci.math)
  • nant and problems compiling
    ... Total time: 0.7 seconds. ... So, not sure where the error lies, if something is missing or if this is ... ..the Sin of Ignorance, ...
    (Fedora)
  • Problem mit acronym-Packet und acf
    ... Die Fehlermeldungen sind unterschiedlich, in einem Fall behauptet LaTeX, zu ... wenig Platz auf dem Stack zu haben, bei untenstehendem Minimalbeispiel ... Missing \endcsname inserted." ...
    (de.comp.text.tex)
  • Re: what is sin?
    ... The Hebrew word "chet" has the same idea of missing the mark. ... to sin; by inference to forfeit, lack, expiate, repent, ... to sin, miss, miss the way, go wrong, incur guilt, forfeit, purify ...
    (uk.religion.christian)