[comp.lang.c] casts and assignments

evil@arcturus.UUCP (Wade Guthrie) (11/30/89)

Let me display my ignorance here: from reading various things posted on the net,
I am led to believe that all assignments include an implicit cast such that:

	a = (type of 'a') (expression);

Is exactly equivalent in ALL cases to:

	a = (expression);

Is this true?


Wade Guthrie
evil@arcturus.UUCP
Rockwell International
Anaheim, CA

(Rockwell doesn't necessarily believe / stand by what I'm saying; how could
they when *I* don't even know what I'm talking about???)

chris@mimsy.umd.edu (Chris Torek) (12/01/89)

In article <6589@arcturus> evil@arcturus.UUCP (Wade Guthrie) writes:
>... I am led to believe that all assignments include an implicit cast
>such that:
>	a = (type of 'a') (expression);
>Is exactly equivalent in ALL cases to:
>	a = (expression);
>Is this true?

Almost.  The result will always be the same [but see below]; however, the
assignment without the cast may be illegal:

	long l;
	char *cp;
	...
	l = cp;		/* should produce at least a warning */
	l = (long)cp;	/* implementation defined, typically no warning */

The result of a cast is (semantically) the same as the result of an
assignment to an unnamed temporary variable with the type given by the
cast, hence

	l = (long)cp;

really means:

	long unnamed_temporary = cp;
	l = unnamed_temporary;

and since both the unnamed temporary and l are long, the assignment is
(probably) just a bit-for-bit copy.  Some simple optimisation will
turn even this (silly) approach into an assignment from cp directly into
l.

One could, however, imagine a machine with the following properties:

	0. there is a -0
	1. there is a valid pointer whose `int' value is -0
	2. copying an int from one register to another with
	   a `move integer' instruction replaces -0 with 0 in
	   the destination.

Then, if the compiler were to do something stupid for

	i = (int)cp;

and generate the sequence

	move pointer cp -> temporary
	move integer temporary -> i

while using the sequence

	move pointer cp -> i

for `i = cp;', the resulting value would be different for the two
assignments.  I am not sure that this is legal, nor am I sure it is
illegal.  It is certainly unlikely.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris

mbrennan@vax1.tcd.ie (12/05/89)

In article <21033@mimsy.umd.edu>, chris@mimsy.umd.edu (Chris Torek) writes:
> In article <6589@arcturus> evil@arcturus.UUCP (Wade Guthrie) writes:
>>... I am led to believe that all assignments include an implicit cast
>>such that:
>>	a = (type of 'a') (expression);
>>Is exactly equivalent in ALL cases to:
>>	a = (expression);
>>Is this true?

int i ;
int j ;
long l ;

	l = i * j ;
	l = (long) i * j ;

I realise I may be answering a slightly different question than you posed, 
but ...

The above two statements will not yeild the same result, because in the 
first instance the int's i and j are multiplied and the result stored in 
an unnamed temp of type int.

In the second case however i is coerced to long first.  Then before
multiplication can take place j is promoted to the same type(long).  This
means the result of the multiplication is held in an unnamed temp of type long
thus ensuring no loss of significant digits.

 ,   ,
Micheal 

mbrennan@cs.tcd.ie

chris@mimsy.umd.edu (Chris Torek) (12/05/89)

[someone's question about  a = (type of 'a') (expression);
 and my reply deleted]

In article <3979@vax1.tcd.ie> mbrennan@vax1.tcd.ie writes:
>int i ;
>int j ;
>long l ;
>
>	l = i * j ;
>	l = (long) i * j ;

>I realise I may be answering a slightly different question than you posed, 
>but ...

You are: the latter is not

	l = (long) (i * j);

but rather

	l = (long) i * j;

which binds as

	l = ((long) i) * j;

Hoping to stem the oncoming tide of confusion (not to mention the
oncoming rerun of how `long=short*short' is the [incorrect] way to
get fast multiplies out of a handful of 680x0 compilers that do
things the hard way for `long=(long)short*short'),
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris

henry@utzoo.uucp (Henry Spencer) (12/06/89)

In article <3979@vax1.tcd.ie> mbrennan@vax1.tcd.ie writes:
>>>... I am led to believe that all assignments include an implicit cast
>>>such that:
>>>	a = (type of 'a') (expression);
>>>Is exactly equivalent in ALL cases to:
>>>	a = (expression);
>>>Is this true?
>
>int i ;
>int j ;
>long l ;
>
>	l = i * j ;
>	l = (long) i * j ;
>
>I realise I may be answering a slightly different question than you posed... 

You are answering a *completely* different question.  Notice that he put
parentheses around the expression, which eliminates the problem you mention
(`(long)i * j' means `((long)i) * j' not `(long)(i * j)').
-- 
Mars can wait:  we've barely   |     Henry Spencer at U of Toronto Zoology
started exploring the Moon.    | uunet!attcan!utzoo!henry henry@zoo.toronto.edu