Why not sizeof int? 
Author Message
 Why not sizeof int?

In the following:
        int x;
        ...
        x = sizeof (x);
        x = sizeof (int);
        x = sizeof x;
        x = sizeof int;
I was just wondering why the last statement is illegal.  Is there some
parsing problem or ambiguity which would have been created if it
had been made legal?

Cheers,
Stan.
--



Mon, 20 Jul 1998 03:00:00 GMT  
 Why not sizeof int?

|        x = sizeof int;
|I was just wondering why the last statement is illegal.  Is there some
|parsing problem or ambiguity which would have been created if it
|had been made legal?

Have a look at the following (syntax violating) expression:

        sizeof int*******p

which causes a tremendous ambiguity, i.e. which star means what here?
On the other hand, the expression:

        sizeof (int***)* ***p

is perfectly readable, i.e. unambiguous, for the parser. (I've included
the single space following the fourth star here for human readability ;-)

kind regards,


--
Atnwgqkrl gy zit vgksr, ug qshiqwtzoeqs!



Tue, 21 Jul 1998 03:00:00 GMT  
 Why not sizeof int?

Quote:

>In the following:
>    int x;
>    ...
>    x = sizeof (x);
>    x = sizeof (int);
>    x = sizeof x;
>    x = sizeof int;
>I was just wondering why the last statement is illegal.  Is there some
>parsing problem or ambiguity which would have been created if it
>had been made legal?

Both.  How would you parse, unambiguously, "sizeof int * + 2"?
Would it be "sizeof(int *) + 2" or "sizeof(int) * +2", both of them being
valid C expressions.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Tue, 21 Jul 1998 03:00:00 GMT  
 Why not sizeof int?

:       x = sizeof int;
: I was just wondering why the last statement is illegal.  Is there some
: parsing problem or ambiguity which would have been created if it
: had been made legal?

If `sizeof typename' were allowed, I think it would require
complicated and/or unintuitive precedence rules to resolve

   sizeof int * - 1

Should it be

   sizeof(int) * -1

or

   sizeof(int *) - 1

?

--
John Rickard



Tue, 21 Jul 1998 03:00:00 GMT  
 Why not sizeof int?

Quote:

> >       x = sizeof int;
> > I was just wondering why the last statement is illegal.  Is there some
> > parsing problem or ambiguity which would have been created if it
> > had been made legal?


Quote:
> If `sizeof typename' were allowed, I think it would require
> complicated and/or unintuitive precedence rules to resolve

>    sizeof int * - 1

Frankly, this does not seem like a convincing point to me in the context
of a language that assumes its programmers can handle things like *p++
and int *p[20];.  One could make an arbitrary choice.  For example, say
that sizeof is grouped with as many tokens to its right as are type-related
keywords, and no more; then the above would be unambiguously sizeof(int)*-1,
while sizeof(int*)-1 would have to be written as it is now.

However, in practice one is more likely to *want* a following * to be
grouped with the sizeof.  Not requiring parentheses is great when the
effect is that the most common grouping can be written without them:
things as complicated as

        if (a < b*c + *p++ && e == f/g + h/i)

are reasonably easy to parse and will be taken by human readers in the same
way as by the compiler.  But the grouping sizeof(int)*-1 is unlikely to be
wanted in practice, whereas expressions like sizeof(int *)-1 are more common
(though that particular one is still unlikely).  So there is little benefit
to be gained from allowing the parentheses to be dropped from the first one.
--

#define MSB(type)       (~(((unsigned type)-1)>>1))

My text in this article is in the public domain.



Wed, 22 Jul 1998 03:00:00 GMT  
 Why not sizeof int?

Quote:

>    x = sizeof int;
>I was just wondering why the last statement is illegal.  Is there some
>parsing problem or ambiguity which would have been created if it
>had been made legal?

It would be impossible to parse e.g.

x = sizeof int**foo ;

Is this sizeof(int*) * foo or sizeof(int) * *foo ?
--
Henning Makholm - math and CS student - University of Copenhagen



Wed, 22 Jul 1998 03:00:00 GMT  
 Why not sizeof int?

Quote:

>    x = sizeof int;
>I was just wondering why the last statement is illegal.  Is there some
>parsing problem or ambiguity which would have been created if it
>had been made legal?

Not just to be contrary ;-), but, I've wondered why the syntax:

     int i;
     x = sizeof i;

is permitted.  Why make a special case when "sizeof(i)" would have
done fine?   Assuming that it was included "so as to not break
existing code", then is it deprecated?  If not, why not?

--




Thu, 23 Jul 1998 03:00:00 GMT  
 Why not sizeof int?

Quote:

> On the other hand, the expression:
>    sizeof (int***)* ***p
> is perfectly readable, i.e. unambiguous, for the parser. (I've
> included the single space following the fourth star here for human
> readability ;-)

Hmm.  I've just tried something close to this under GCC on Linux, and
it's parsed as you say.  My question is: why is it tokenized as

        (sizeof (int***)) times (***p)

rather than

        sizeof ((int***)(****p))

that is, as a cast of (****p)?

--
                             Tim McDaniel





Thu, 23 Jul 1998 03:00:00 GMT  
 Why not sizeof int?

Quote:

>Not just to be contrary ;-), but, I've wondered why the syntax:

>     int i;
>     x = sizeof i;

>is permitted.  Why make a special case when "sizeof(i)" would have
>done fine?   Assuming that it was included "so as to not break
>existing code", then is it deprecated?  If not, why not?

sizeof comes in two flavours:

        sizeof expression
        sizeof ( type-name )

sizeof(i) belongs to the first flavour, the expression being (i) while
sizeof(int) belongs to the second flavour.  So, "sizeof i" is no special
case, it's an ordinary instance of the first flavour.  Neither flavour is
deprecated, both of them being very useful.

What you're asking for is a change in the syntax of the first flavour, so
that it becomes:

        sizeof ( expression )

Such a change would indeed break a lot of existing code without offering
any advantage to compensate for this.  It ain't gonna happen.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Fri, 24 Jul 1998 03:00:00 GMT  
 Why not sizeof int?
[ Moderator: Please check if this is a duplicate ... I think my line went
down before I could send it off (and ate up my post :-(. I am
re-explaining the issue. ]


<snip>
   >         sizeof (int***)* ***p
<snip>
   Hmm.  I've just tried something close to this under GCC on Linux, and
   it's parsed as you say.  My question is: why is it tokenized as

It is not a question of `tokenization'. The tokens are obviously the
following:

sizeof ( int * * * ) * * * * p

It is how the tokens combine to form an expression.

           (sizeof (int***)) times (***p)

   rather than

           sizeof ((int***)(****p))

   that is, as a cast of (****p)?

The C standard uses a syntax which makes this kind of ambiguity
obsolete. To wit, some of the ways of producing an expression can be
read off from the following syntax. Note that the syntax determines
how it is to be interpreted: whether that interpretation is acceptable
is further restricted by constraints and other sections of the
standard. In short,

1) If exactly one interpretation is allowed by the syntax it is chosen.
2) If this interpretation violates constraints, a diagnostic is
   issued.
3) If no interpretation is allowed by the syntax, a diagnostic is
   issued.
4) If more than one interpretation is allowed by the syntax, the
   standard is modified. (There may be a few exceptions: the dangling
   if problem is one where some `semantic' rules are used in
   ambiguity resolution.)

(The following is a _very_ small part of the syntax)

Any identifier can be a `primary expression'.
Any expression in parenthesis can be a `primary expression'.
Any primary expression can be a `postfix expression'
Any postfix expression followed by ++ can be a `postfix expression'
A postfix expression can be a `unary expression'
* followed by a cast expression is a `unary expression'.
sizeof followed by a unary expression can be a `unary expression'
sizeof of followed by a type-name in parentheses can be a
          `unary expression'
A unary expression can be a `cast expression'
A type-name in parentheses followed by a cast expression can be a
           `cast expression'

and so on, so that a `cast expression' can be an `expression'.

Note that the clause `sizeof followed by a cast expression' never
appears in the above set of rules. Also note that a `cast expression'
never becomes a `unary expression'. So, they syntax does not allow the
interpretation that sizeof is acting on a cast.

On the other hand,  sizeof ((int***)(****p)) (with the parenthesis) is
completely valid. It is `parsed' as

p (identifier) --> primary ---> postfix ---> unary ---> cast
* followed by above ---> unary --> cast
* followed by above ---> unary --> cast
* followed by above ---> unary --> cast
* followed by above ---> unary --> cast --> ... ---> expression
above in parentheses ---> primary ---> postfix ---> unary ---> cast
int*** (type-name) in parentheses followed by above ---> cast
above in parentheses ---> primary ---> postfix ---> unary
sizeof followed by above ---> unary ---> cast --> ... --->expression

I hope you can now see how these things are disambiguated.

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Fri, 24 Jul 1998 03:00:00 GMT  
 Why not sizeof int?

Quote:

>Not just to be contrary ;-), but, I've wondered why the syntax:
>     int i;
>     x = sizeof i;
>is permitted.  Why make a special case when "sizeof(i)" would have
>done fine?   Assuming that it was included "so as to not break
>existing code", then is it deprecated?  If not, why not?

Why in the name of sanity _should_ it be deprecated?
If you see
        sizeof i
you *know* that i must be an object identifier, not a type identifier.
In your own code, when you are intimately familiar with all the ten
thousand identifiers and their meanings, you may not care.  In other
people's code, the visual clue can be very helpful.

The important thing to understand is that sizeof IS NOT A FUNCTION.
It's an operator.  It requires no parentheses of its own.  In the
same way, you are allowed to write either of
1.      i = 1;
2.      (i) = 1;
Do you want i = 1 deprecated as well?

People who always use parentheses with sizeof regardless of whether the
operand is a type or an object are being
"fools to themselves and burdens to others".

--
"conventional orthography is ... a near optimal system for the
 lexical representation of English words." Chomsky & Halle, S.P.E.
Richard A. O'Keefe; http://www.cs.rmit.edu.au/~ok; RMIT Comp.Sci.



Fri, 24 Jul 1998 03:00:00 GMT  
 Why not sizeof int?

Quote:

>Not just to be contrary ;-), but, I've wondered why the syntax:

>     int i;
>     x = sizeof i;

>is permitted.  Why make a special case when "sizeof(i)" would have
>done fine?   Assuming that it was included "so as to not break
>existing code", then is it deprecated?  If not, why not?

'sizeof <x>' is a unary expression, like 'return <x>'(*), casts, etc.  It is
most definately not a function (or macro), so requiring parenthesis might
cause confusion... (which header defines 'sizeof' again :-).

Has anyone else noticed that anywhere that a type name is allowed (and it's
not in a declaration/definition) it always is surrounded by parenthesis?

Casts and sizeof are the only 2 situations that come to mind, are there more
places a type name (that isn't declaring/defining something) is allowed?



Sat, 25 Jul 1998 03:00:00 GMT  
 Why not sizeof int?


Quote:
>Casts and sizeof are the only 2 situations that come to mind, are there more
>places a type name (that isn't declaring/defining something) is allowed?

offsetof()

        J



Mon, 27 Jul 1998 03:00:00 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Why is an int not an int?

2. relation between sizeof(void*), sizeof(int)

3. sizeof(int) sizeof(long) etc.

4. Why does fgets use int and not size_t?

5. Why not "const char *strerror(int);"??

6. Why does setw(int) not work?

7. BOOL: why int and not unsigned char ?

8. Why is extern int * buf != extern int buf[] ?

9. Why does Param list not show for void __stdcall MyFUNC(int x,int y,int z)?

10. /Wp64 and sizeof int question

11. how to modify a pointer to an array one sizeof(int)

12. sizeof(int)

 

 
Powered by phpBB® Forum Software