Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sizeof of expression & sizeof of type

Reply
Thread Tools

sizeof of expression & sizeof of type

 
 
Alex Vinokur
Guest
Posts: n/a
 
      06-10-2006
Why does one need to use two kinds of sizeof operator:
* sizeof unary-expression,
* sizeof (type-name)
?

Their behavior seem not to be different (see an example below).

------ C++ code ------
#include <iostream>
using namespace std;

int main()
{
int x = 100;

cout << sizeof ++x << endl; // expression sizeof
cout << x << endl;

x = 100;
cout << endl;
cout << sizeof (++x) << endl; // type sizeof
cout << x << endl;

return 0;
}
----------------------

------ Run ------

4
100

4
100

-----------------

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn



 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      06-10-2006
Alex Vinokur wrote:

> Why does one need to use two kinds of sizeof operator:
> * sizeof unary-expression,
> * sizeof (type-name)
> ?


Because you might want to use it on a value to find out its size, or you
want to use it on a type to find out its size.

>
> Their behavior seem not to be different (see an example below).
>
> ------ C++ code ------
> #include <iostream>
> using namespace std;
>
> int main()
> {
> int x = 100;
>
> cout << sizeof ++x << endl; // expression sizeof
> cout << x << endl;
>
> x = 100;
> cout << endl;
> cout << sizeof (++x) << endl; // type sizeof


(++x) is not a type. int would be a type.

> cout << x << endl;
>
> return 0;
> }
> ----------------------
>
> ------ Run ------
>
> 4
> 100
>
> 4
> 100


 
Reply With Quote
 
 
 
 
Alex Vinokur
Guest
Posts: n/a
 
      06-10-2006

"Rolf Magnus" <> wrote in message news:e6eihq$66v$03$...
> Alex Vinokur wrote:

[snip]
> >
> > int main()
> > {
> > int x = 100;
> >
> > cout << sizeof ++x << endl; // expression sizeof
> > cout << x << endl;
> >
> > x = 100;
> > cout << endl;
> > cout << sizeof (++x) << endl; // type sizeof

>
> (++x) is not a type. int would be a type.

[snip]

I think, ++x is parsed as typename in sizeof (++x).


--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn



 
Reply With Quote
 
Markus Schoder
Guest
Posts: n/a
 
      06-10-2006
Alex Vinokur wrote:
> "Rolf Magnus" <> wrote in message news:e6eihq$66v$03$...
> > Alex Vinokur wrote:

> [snip]
> > >
> > > int main()
> > > {
> > > int x = 100;
> > >
> > > cout << sizeof ++x << endl; // expression sizeof
> > > cout << x << endl;
> > >
> > > x = 100;
> > > cout << endl;
> > > cout << sizeof (++x) << endl; // type sizeof

> >
> > (++x) is not a type. int would be a type.

> [snip]
>
> I think, ++x is parsed as typename in sizeof (++x).


No it is a unary-expression because a primary-expression is also a
unary-expression and ( expression ) is a primary-expression and clearly
++x is an expression.

 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      06-10-2006

Markus Schoder wrote:
> Alex Vinokur wrote:
> > "Rolf Magnus" <> wrote in message news:e6eihq$66v$03$...
> > > Alex Vinokur wrote:

> > [snip]
> > > >
> > > > int main()
> > > > {
> > > > int x = 100;
> > > >
> > > > cout << sizeof ++x << endl; // expression sizeof
> > > > cout << x << endl;
> > > >
> > > > x = 100;
> > > > cout << endl;
> > > > cout << sizeof (++x) << endl; // type sizeof
> > >
> > > (++x) is not a type. int would be a type.

> > [snip]
> >
> > I think, ++x is parsed as typename in sizeof (++x).

>
> No it is a unary-expression because a primary-expression is also a
> unary-expression and ( expression ) is a primary-expression and clearly
> ++x is an expression.


What is the difference between
sizeof ++x
and
sizeof (++x)
?

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      06-10-2006
Alex Vinokur wrote:

> What is the difference between
> sizeof ++x
> and
> sizeof (++x)
> ?


Nothing. sizeof takes an expression, and parens in expressions may be
optional.

sizeof(int) is the size of the typecast to int. (int)0.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!


 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      06-10-2006

Phlip wrote:
> Alex Vinokur wrote:
>
> > What is the difference between
> > sizeof ++x
> > and
> > sizeof (++x)
> > ?

>
> Nothing. sizeof takes an expression, and parens in expressions may be
> optional.


So, why do we need two kinds of sizeof operator?

>
> sizeof(int) is the size of the typecast to int. (int)0.


[snip]

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

 
Reply With Quote
 
Markus Schoder
Guest
Posts: n/a
 
      06-10-2006
Alex Vinokur wrote:
> Phlip wrote:
> > Alex Vinokur wrote:
> >
> > > What is the difference between
> > > sizeof ++x
> > > and
> > > sizeof (++x)
> > > ?

> >
> > Nothing. sizeof takes an expression, and parens in expressions may be
> > optional.

>
> So, why do we need two kinds of sizeof operator?


One takes an expression and returns the size of the expression's type
and one takes a type directly (which must be put in parentheses). It is
mostly a matter of convenience.

 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      06-11-2006
Alex Vinokur schrieb:
> Phlip wrote:
>> Alex Vinokur wrote:
>>
>>> What is the difference between
>>> sizeof ++x
>>> and
>>> sizeof (++x)
>>> ?

>> Nothing. sizeof takes an expression, and parens in expressions may be
>> optional.

>
> So, why do we need two kinds of sizeof operator?


int x;

1) sizeof(int)
2) sizeof(x) (or sizeof x)

Which one do you think is redundant?

Thomas
 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      06-11-2006

Thomas J. Gritzan wrote:
[snip]
>
> int x;
>
> 1) sizeof(int)
> 2) sizeof(x) (or sizeof x)
>
> Which one do you think is redundant?
>


1) sizeof(int)
2) sizeof(x)
3) sizeof x // redundant

[snip]

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
#define ALLOCIT(Type) ((Type*) malloc (sizeof (Type))) Yevgen Muntyan C Programming 10 02-13-2007 02:52 AM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments