sizeof of expression & sizeof of type

A

Alex Vinokur

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
 
R

Rolf Magnus

Alex said:
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.
 
A

Alex Vinokur

Rolf Magnus said:
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).
 
M

Markus Schoder

Alex said:
Rolf Magnus said:
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.
 
A

Alex Vinokur

Markus said:
Alex said:
Rolf Magnus said:
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
 
P

Phlip

Alex said:
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.
 
M

Markus Schoder

Alex said:
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.
 
T

Thomas J. Gritzan

Alex said:
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
 
D

Default User

Alex said:
Thomas J. Gritzan wrote:

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

You are confused. 2 and 3 are the same thing, there just are
superfluous parentheses in 2. It's somewhat similar, although not
exactly, to this:


return (2);

return 2;



Those aren't redundant forms of return, just unneeded parentheses.




Brian
 
S

Steve Pope

Default User said:
Alex Vinokur wrote:
You are confused. 2 and 3 are the same thing, there just are
superfluous parentheses in 2. It's somewhat similar, although not
exactly, to this:
return (2);
return 2;

Not exactly indeed, due to operator precedence.

int x;
long long y;
int z = sizeof x, y;

Gives you 4, while

int x=1;
int y=2;
int z = return 1, 2;

Gives you 2.

Steve
 
D

Default User

Steve said:
Not exactly indeed, due to operator precedence.

int x;
long long y;
int z = sizeof x, y;

Gives you 4, while

int x=1;
int y=2;
int z = return 1, 2;

Gives you 2.

What does any of that have to do with parentheses? The OP didn't
mention the comma operator, nor did I.



Brian
 
S

Steve Pope

Default User said:
Steve Pope wrote:
What does any of that have to do with parentheses? The OP didn't
mention the comma operator, nor did I.

The "sizeof" operator has a certain precedence. (Look at
the table of operator precedences e.g. in Stroustrup.) The "return"
statement is not an operator, therefore has lower precedence
than any operator, therefore the entire expression to the right
of it is evaluated first.

That's how I look at it, in any case.

Steve
 
D

Default User

Steve said:
The "sizeof" operator has a certain precedence.
That's how I look at it, in any case.

Hence the qualifier, "It's somewhat similar, although not exactly, to
this". Yeah, we can construct some examples where the () could matter,
but they don't for the ones the OP presented.



Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top