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
15 2248
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
Default User <de***********@ yahoo.com> 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.
Steve
Steve Pope wrote: Default User <de***********@ yahoo.com> 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
Default User <de***********@ yahoo.com> wrote: Steve Pope wrote:
Default User <de***********@ yahoo.com> 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.
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
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)))
....
Steve Pope wrote: Default User <de***********@ yahoo.com> 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 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: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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: 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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |