473,786 Members | 2,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof of expression & sizeof of type

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

Jun 10 '06 #1
15 2247
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


Jun 10 '06 #2

"Rolf Magnus" <ra******@t-online.de> 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
Alex Vinokur wrote:
"Rolf Magnus" <ra******@t-online.de> 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

Markus Schoder wrote:
Alex Vinokur wrote:
"Rolf Magnus" <ra******@t-online.de> 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
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

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
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
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

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

70
8913
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this expression should equal to "sizeof( (int)(*p) )", but the compiler does NOT think so. Why? Can anyone help me? Thanks. Best regards. Roy
2
2469
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
9
3025
by: M Welinder | last post by:
This doesn't work with any C compiler that I can find. They all report a syntax error: printf ("%d\n", (int)sizeof (char)(char)2); Now the question is "why?" "sizeof" and "(char)" have identical precedence and right-to-left parsing, so why isn't the above equivalent to printf ("%d\n", (int)sizeof ((char)(char)2));
7
1939
by: dam_fool_2003 | last post by:
#include<stdio.h> int main(void) { unsigned int a=20,b=50, c = sizeof b+a; printf("%d\n",c); return 0; } out put: 24
42
2416
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
90
8493
by: pnreddy1976 | last post by:
Hi, How can we write a function, which functionality is similar to sizeof function any one send me source code Reddy
28
6758
by: Howard Bryce | last post by:
I have come across code containing things like sizeof int How come that one can invoke sizeof without any parentheses surrounding its argument? Is this admissible within the standard? Can it in general be done with one argument functions? Why would one want to do it anyway, other than showing that one knows and to save two characters?
11
1595
by: Richard Tobin | last post by:
Please excuse me if this has already been covered. Given char x; is sizeof(x)
12
2747
by: Ioannis Vranos | last post by:
In C99 it is mentioned: "The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type.". If I am not wrong, this implies that int x;
0
9650
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8992
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.