449,161 Members | 1,091 Online Need help? Post your question and get tips & solutions from a community of 449,161 IT Pros & Developers. It's quick & easy.

# sizeof of expression & sizeof of type

 P: n/a 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 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 Jun 10 '06 #1
15 Replies

 P: n/a 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 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 Jun 10 '06 #2

 P: n/a "Rolf Magnus" wrote in message news:e6*************@news.t-online.com... 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 Jun 10 '06 #3

 P: n/a Alex Vinokur wrote: "Rolf Magnus" wrote in message news:e6*************@news.t-online.com... 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. Jun 10 '06 #4

 P: n/a Markus Schoder wrote: Alex Vinokur wrote: "Rolf Magnus" wrote in message news:e6*************@news.t-online.com... 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 Jun 10 '06 #5

 P: n/a 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!!! Jun 10 '06 #6

 P: n/a 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 Jun 10 '06 #7

 P: n/a 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. Jun 10 '06 #8

 P: n/a 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 Jun 11 '06 #9

 P: n/a 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 Jun 11 '06 #10

 P: n/a Alex Vinokur wrote: Thomas J. Gritzan wrote: Which one do you think is redundant? 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 Jun 11 '06 #11

 P: n/a Default User wrote: Alex Vinokur wrote: Thomas J. Gritzan wrote: > Which one do you think is redundant? 1) sizeof(int) 2) sizeof(x) 3) sizeof x // redundant You are confused. 2 and 3 are the same thing, there just aresuperfluous parentheses in 2. It's somewhat similar, although notexactly, 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 Jun 12 '06 #12

 P: n/a Steve Pope wrote: Default User wrote: Alex Vinokur wrote: Thomas J. Gritzan wrote: Which one do you think is redundant? 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; 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 Jun 12 '06 #13

 P: n/a Default User wrote: Steve Pope wrote: Default User wrote: > Alex Vinokur wrote: >> Thomas J. Gritzan wrote: >> > Which one do you think is redundant? >> 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; 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'tmention 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 Jun 12 '06 #14

 P: n/a Alex Vinokur wrote: 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 you forgot 4) sizeof ((x)) 5) sizeof (((x))) .... Jun 12 '06 #15

 P: n/a Steve Pope wrote: Default User 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. 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 Jun 12 '06 #16

### This discussion thread is closed

Replies have been disabled for this discussion. 