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 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
"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
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.
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
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!!!
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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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"; }
|
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));
|
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
|
by: Christopher C. Stacy |
last post by:
Some people say sizeof(type) and other say sizeof(variable).
Why?
| |
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
|
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?
|
by: Richard Tobin |
last post by:
Please excuse me if this has already been covered.
Given
char x;
is
sizeof(x)
|
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;
|
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...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |