What does the expression "sizeof(int)*p" mean?

R

Roy Yao

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
 
Y

Y. Keerthi Sagar

Roy Yao said:
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




Hey,
Both "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" are not same.
"(sizeof(int))* (p)" means you are multiplying sizeof(int), i.e. 2
(bytes), with the value of "p". Whereas "sizeof( (int)(*p) )" means,
First you are getting the value of at the adderess p (because *p gives
the value of another variable), then you are converting the returned
value into interger format and finally you are detecting the size of
that result. If you have any doubts contact at my ID
(e-mail address removed).

Be Cool,
Y. Keerthi
Sagar & Ashok.
 
R

Roy Yao

Hello Keerthi,

Thanks for your enthusiasm.

My email to you was rejected by your mail server, so I reply you here.

In this expression, what confused me is how the compilers (or ANSI C)
determine the combination of operator sizeof, (type) and *.

In my opion it should equal to expression "sizeof( (int)(*p) )". The
reason is that operator sizeof, (type) and * have the same
precedence, and they combine from right to left. But the compilers prefer
"(sizeof(int))* (p)".

Now can you give me a convictive explain from the precedence and
combination order of operator ?

Thanks again.

Best regards,
Roy Yao
 
J

Josh Sebastian

Hello Keerthi,

Thanks for your enthusiasm.

My email to you was rejected by your mail server, so I reply you here.

In this expression, what confused me is how the compilers (or ANSI C)
determine the combination of operator sizeof, (type) and *.

In my opion it should equal to expression "sizeof( (int)(*p) )". The
reason is that operator sizeof, (type) and * have the same
precedence, and they combine from right to left. But the compilers prefer
"(sizeof(int))* (p)".

Now can you give me a convictive explain from the precedence and
combination order of operator ?

I told you in my other post, that's NOT the (type) operator! (type) is the
/operand/ of sizeof.

Josh
 
W

wang tianxing

: On Tue, 16 Sep 2003 08:44:02 +0800, Roy Yao wrote:
:
: > Hello Keerthi,
: >
: > Thanks for your enthusiasm.
: >
: > My email to you was rejected by your mail server, so I reply you here.

You(Roy) are supposed to reply here.

: > In this expression, what confused me is how the compilers (or ANSI C)
: > determine the combination of operator sizeof, (type) and *.
: >
: > In my opion it should equal to expression "sizeof( (int)(*p) )". The
: > reason is that operator sizeof, (type) and * have the same
: > precedence, and they combine from right to left. But the compilers prefer
: > "(sizeof(int))* (p)".
: >
: > Now can you give me a convictive explain from the precedence and
: > combination order of operator ?
:
: I told you in my other post, that's NOT the (type) operator! (type) is the
: /operand/ of sizeof.

You are right, but he wants to know why. :)

Some relevent production rules:

unary-expression:
postfix-expression
++ cast-expression
-- cast-expression
unary-operator cast-expression
sizeof unary-expression
sizeof ( type-id )
new-expression
delete-expression

unary-operator: one of
* & + - ! ~

The expression in question:

sizeof (int) * p

'(int) * p' isn't a unary-expression, because it is not starting
with ++, --, *, &, +, -, !, ~, sizeof, new, or delete, and it is
not a postfix-expression (believe me!). So a compiler can't parse
the expression in question as 'sizeof unary-expression'.


Regards,
tx
 
J

jeffc

Roy Yao said:
Hello Keerthi,

Thanks for your enthusiasm.

My email to you was rejected by your mail server, so I reply you here.

In this expression, what confused me is how the compilers (or ANSI C)
determine the combination of operator sizeof, (type) and *.

In my opion it should equal to expression "sizeof( (int)(*p) )". The
reason is that operator sizeof, (type) and * have the same
precedence, and they combine from right to left. But the compilers prefer
"(sizeof(int))* (p)".

Now can you give me a convictive explain from the precedence and
combination order of operator ?

sizeof is a function, and the parameter it takes is whatever is inside the
parens. If you wrote
sizeof((int)*p), you'd evaluate the expression inside the outer parens
first. You cast whatever
*p is to an int, then take the size of that. Same as sizeof(int). But what
you wrote in your subject
is not exactly the same as either of the 2 options in your text message.
 
K

Kevin Goodsell

jeffc said:
sizeof is a function,

No, it's not. It is an operator.
and the parameter it takes is whatever is inside the
parens. If you wrote
sizeof((int)*p), you'd evaluate the expression inside the outer parens
first.

No, sizeof's operand is not evaluated.

-Kevin
 
X

Xenos

jeffc said:
sizeof is a function, and the parameter it takes is whatever is inside the
parens. If you wrote
sizeof((int)*p), you'd evaluate the expression inside the outer parens
first. You cast whatever
*p is to an int, then take the size of that. Same as sizeof(int). But what
you wrote in your subject
is not exactly the same as either of the 2 options in your text message.

No, sizeof is an operator. The paranteses are only required for uses with a
type expression. Any other use only need them because of precedence.
Examples are:

sizeof(int)
sizeof 12345
sizeof 'a'
sizeof (a + b)

DrX.
 
X

Xenos

jeffc said:
sizeof is a function, and the parameter it takes is whatever is inside the
parens. If you wrote
sizeof((int)*p), you'd evaluate the expression inside the outer parens
first. You cast whatever
*p is to an int, then take the size of that. Same as sizeof(int). But what
you wrote in your subject
is not exactly the same as either of the 2 options in your text message.

Additionally, his second option: (sizeof(int)*(p)) is exactly the same as
the one in his subject line.


DrX.
 
R

Ron Natalie

jeffc said:
sizeof is a function,

sizeof is not a function. It's an operator.
and the parameter it takes is whatever is inside the
parens.

Sorry untrue. The parens aren't strictly requird. The grammar of sizeof
is:

sizeof unary-expression // note no parens here.
swizeof ( type-id )
you'd evaluate the expression inside the outer parens
first.

Parens have no bearing on order of evaulation in C++.
 
W

WW

jeffc said:
Makes no difference.

You stated that sizeof is a function. It is not. It is an operator. Makes
all the difference possible. It is an operator, same way as the + sign ot
the () function call operator.
 
J

jeffc

Ron Natalie said:
sizeof is not a function. It's an operator.

Same thing.
Sorry untrue. The parens aren't strictly requird.

I didn't say they were required. I said it takes whatever is inside the
parens. Are you arguing with the term "parameter"? I could easily argue
that all operators take parameters (or arguments). This is more clear in
C++ than C.
Parens have no bearing on order of evaulation in C++.

Of course they do. Compare:
1 + (2 * 3)
(1 + 2) * 3

But that's beside the point. What I wrote was "If you wrote
sizeof((int)*p), you'd evaluate the expression inside the outer parens
first." That is true. Let's say p were of type double*. We are taking the
size of an int, not double, and not int multiplied by double.
 
J

jeffc

WW said:
You stated that sizeof is a function. It is not. It is an operator. Makes
all the difference possible. It is an operator, same way as the + sign ot
the () function call operator.

Operators are functions. It's just that the syntax is sometimes a little
different. It sure does not make "all the difference possible". Why do you
think the term "Operator Functions" exists?
 
W

WW

jeffc said:
Same thing.

No, it is not. See the standard. See any language book. Please explain if
operator is same as function, that how the heck can a *function call
operator* exist?
 
W

WW

jeffc said:
Operators are functions. It's just that the syntax is sometimes a
little different. It sure does not make "all the difference
possible". Why do you think the term "Operator Functions" exists?

Jeff it is not a shame to be a beginner, but it is a shameful to see years
later your incorrect statements still in the newsgroup archives. Been
there. So making the mistake of calling operators as functions is one
thing. Insisting on it when you have been told it is not true is another
thing.

In 13.5 Oveerloaded operators Paragraph one Simon says:

"A function declaration having one of the following /operator-function-ids/
as its name declares an operator function. An operator function is said to
implement the operator named in its /operator-function-ids/."

Read it carefully. It does not say operators are functions. It says:
overloaded operators (overloaded ones!) are *implemented as* functions.
 
J

jeffc

WW said:
No, it is not. See the standard. See any language book.

I obviously don't mean an operator is EXACTLY the same thing as a function.
I mean for the purposes of what I was saying, and in context, it amounts to
the same thing. int and long are 2 different things, but in some contexts
they are the same thing. On some machines they take the same amount
storage, on others they might not. In some operations they yield the same
results, in others they do not.

This is the same silly argument as when Ron tried to argue that calls to
base class constructors from initializer lists are not function calls merely
because the physical order they appear in don't mean that's the order
they'll necessarily be called in.

It doesn't matter if you write
sizeof 'a'
sizeof('a')
The results are the same.
 
J

jeffc

WW said:
"A function declaration having one of the following /operator-function-ids/
as its name declares an operator function. An operator function is said to
implement the operator named in its /operator-function-ids/."

Read it carefully. It does not say operators are functions. It says:
overloaded operators (overloaded ones!) are *implemented as* functions.

It also says "a function declaration". What does that mean? And does that
prove anything? You're being silly. Operators take arguments. They act
like functions. If the context of this discussion were different, you'd be
arguing the opposite side just as vehemently. You know exactly what I'm
talking about, so you're just being arbitrary.
 
W

WW

jeffc said:
I obviously don't mean an operator is EXACTLY the same thing as a
function. I mean for the purposes of what I was saying, and in
context, it amounts to the same thing.

You have said for the sizeof operator to be a function. It is not. It can
be compared to one (knowing the differences) but it is not a function from
*any* valid viewpoint.
int and long are 2 different
things, but in some contexts they are the same thing.

In no context they are the same thing. They both belong to types if that is
what you mean but functions and the sizeof operators only meet at the
expression level. Functions are called or inlined while the implementation
for operators is not defined etc. They are not the same in any repspect.
Especially when talking about sizeof.
On some
machines they take the same amount storage, on others they might not.

Which has no significance in the matter of operators are not functions.
They are both integral types, their coherence is much much higher than the
one between operators and functions.
In some operations they yield the same results, in others they do not.

Irrelevant to the topic.
This is the same silly argument as when Ron tried to argue that calls
to base class constructors from initializer lists are not function
calls merely because the physical order they appear in don't mean
that's the order they'll necessarily be called in.

Sure. They are not function calls. There are several reasons to it. One
of ehich Ron told. Another (which leads to the same) is that contsructors
are not functions. They are *special* member functions, which special
semantics.
It doesn't matter if you write
sizeof 'a'
sizeof('a')
The results are the same.

Yes. How does that support your theory of sizeof being a function from the
C++ language point of view?
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top