Regarding sizeof Operator

S

sonu

#include<stdio.h>
main()
{
int x=10,y;
y=sizeof(++x);

printf("x=%d\ny=%d\n",x,y);
}

Oput Put

x=10
y=4

why not x=11
 
R

Richard Heathfield

sonu said:
#include<stdio.h>
main()
{
int x=10,y;
y=sizeof(++x);

printf("x=%d\ny=%d\n",x,y);
}

Oput Put

x=10
y=4

why not x=11

Because sizeof does not evaluate its operand.
 
V

Vladimir S. Oka

#include<stdio.h>
main()
{
int x=10,y;
y=sizeof(++x);

printf("x=%d\ny=%d\n",x,y);
}

Oput Put

x=10
y=4

why not x=11

Do not post the same question every 6 minutes, especially not with the
different subject line (it makes it difficult to ignore). It is only
reasonable to assume your post is not seen after at least 24 hours.
This is not a chat room, this is Usenet. Look up what it is and it's
history. GIYF.

In any case, you did get two replies very quickly.

--
BR, Vladimir

Latin is a language,
As dead as can be.
First it killed the Romans,
And now it's killing me.
 
R

Richard G. Riley

#include<stdio.h>
main()
{
int x=10,y;
y=sizeof(++x);

printf("x=%d\ny=%d\n",x,y);
}

Oput Put

x=10
y=4

why not x=11

"sizeof" operator does not evaluate the expression contained therein
it seems. I didnt know that : but purely because I never stuck an
expression in it for reasons unknown - I just assumed it as a
"preprocessor" type thing and just never did it. I can claim no genius.

It would be the same had you put

sizeof(x=3);

I would agree that it is a little confusing since the
compiler (gcc) happily lets you write something like

sizeof(x=funcCall(y)) too!

Looking at this in n1124pdf:

6.5.3.4 The sizeof operator
Constraints

2 The sizeof operator yields the size (in bytes) of its operand, which
may be an expression or the parenthesized name of a type. The size is
determined from the type of the operand. The result is an integer. If
the type of the operand is a variable length array type, the operand
is evaluated; otherwise, the operand is not evaluated and the result
is ....

There is a rather unhelpful footnote which states

"the operand of a sizeof operator is usually not evaluated"

Hopefully someone will enlighten us both as to what the standard means
: I just assumed it was a pre-processor type thing and would never
have thought the compiler would allow such a statement there.
 
R

Richard Heathfield

Richard G. Riley said:
"sizeof" operator does not evaluate the expression contained therein
it seems.
Correct.

I didnt know that :
Evidently.

but purely because I never stuck an
expression in it for reasons unknown - I just assumed it as a
"preprocessor" type thing and just never did it.

It is nothing to do with the preprocessor.

I can claim no genius.
Evidently.


It would be the same had you put

sizeof(x=3);
Correct.


I would agree that it is a little confusing

I wouldn't.
since the
compiler (gcc) happily lets you write something like

sizeof(x=funcCall(y)) too!

It's supposed to.
Looking at this in n1124pdf:

6.5.3.4 The sizeof operator
Constraints

2 The sizeof operator yields the size (in bytes) of its operand, which
may be an expression or the parenthesized name of a type. The size is
determined from the type of the operand. The result is an integer. If
the type of the operand is a variable length array type, the operand
is evaluated; otherwise, the operand is not evaluated and the result
is ....

There is a rather unhelpful footnote which states

"the operand of a sizeof operator is usually not evaluated"

That is merely an expansion of the normative text. It adds no particular
value.
Hopefully someone will enlighten us both as to what the standard means
: I just assumed it was a pre-processor type thing

sizeof is a C keyword, not a preprocessor directive.
and would never
have thought the compiler would allow such a statement there.

Evidently. And yet it does, because it must.

What you think does not necessarily equate to what is true.
 
P

pemo

Richard said:
Richard G. Riley said:




That is merely an expansion of the normative text. It adds no
particular value.

Although sizeof must evaluate the size of a variable length array under c99
rules ... maybe it's a vague reference to that - I believe it's the only
case in which sizeof /evaluates/?

<snip>
 
R

Richard G. Riley

Although sizeof must evaluate the size of a variable length array under c99
rules ... maybe it's a vague reference to that - I believe it's the only
case in which sizeof /evaluates/?

<snip>

I just found the relevant part in K&R2 A7.4.8 : it states that expressions
are not evaluated. While I did download n1124.pdf, I always find it
hard to find something there and all I did find I documented in
that post. I'm sure KT or somewould more aufait with the document can
quote the relevant part.
 
R

Robert Gamble

Richard said:
I just found the relevant part in K&R2 A7.4.8 : it states that expressions
are not evaluated.

They weren't when K&R2 was published. K&R2 doesn't cover C99.
While I did download n1124.pdf, I always find it
hard to find something there and all I did find I documented in
that post. I'm sure KT or somewould more aufait with the document can
quote the relevant part.

(It's "au fait" by the way, two words)

Did you even read the section you posted?

"If the type of the operand is a variable length array type, the
operand
is evaluated; otherwise, the operand is not evaluated ..."

What isn't clear about that?

Robert Gamble
 
R

Richard G. Riley

They weren't when K&R2 was published. K&R2 doesn't cover C99.


(It's "au fait" by the way, two words)

Did you even read the section you posted?

<sarcasm on> No. I posted it totally blindly just guessing it had
something to do with the OP. I guess I was luck I hit on something to
"If the type of the operand is a variable length array type, the
operand
is evaluated; otherwise, the operand is not evaluated ..."

What isn't clear about that?

The footnote I also cited?

"the operand of a sizeof operator is usually not evaluated"

As I said, I'm not so au fait with this stuff and sometimes have
difficulty translating it too, and the word "usually" left some doubt
in my mind.

I did state that I wasnt sure? Didnt I?

But thanks for the clarification anyway.
 
R

Richard Heathfield

pemo said:
Although sizeof must evaluate the size of a variable length array under
c99 rules ...

Yes, and the normative text explains that perfectly clearly.
 
R

Richard Heathfield

Richard G. Riley said:
<sarcasm on> No. I posted it totally blindly just guessing it had
something to do with the OP. I guess I was luck I hit on something to
do with sizeof eh? <sarcasm off>

I see very little sarcasm there.
 
J

Jordan Abel

Richard G. Riley said:


It is nothing to do with the preprocessor.



I wouldn't.


It's supposed to.

Out of curiosity, is

sizeof cos() [no prototype in scope] allowed [sizeof(int)], or is it still
undefined behavior?

That is, is it the evaluation of a function that the compiler has
"wrong" what causes undefined behavior, or is it the presence in the
source of the call itself?

how about

sizeof(((int(*)())0)())?
 
M

Micah Cowan

Jordan Abel said:
Out of curiosity, is

sizeof cos() [no prototype in scope] allowed [sizeof(int)], or is it still
undefined behavior?

Well, implicit declarations were removed in C99, so it's a constraint
violation.

I was going to say "it's fine" for C90, but I gave it a little more
thought after your next sentence. Because, whether or not it is
actually evaluated, it results in the implicit declaration.
That is, is it the evaluation of a function that the compiler has
"wrong" what causes undefined behavior, or is it the presence in the
source of the call itself?

It is the declaration of cos() that causes undefined behavior. cos is
a reserved identifier (7.1.3#1), and is only allowed to refer to the
standard library function. Since your cos does not, it violates
this rule. Rules 6.2.2#2 and 6.2.7#2 apply when it's a function you've
defined yourself. These are C99 references, but they appear
equivalently in C90.

However, in C90, sizeof(foo()) would be allowed (if you never define
foo()). The implicit declaration is fine, because C90 3.7
(corresponding to C99 6.9) only requires a definition to exist if you
use its identifier in an expression other than the operand of a sizeof.
how about

sizeof(((int(*)())0)())?

Perfectly fine. The expression is never evaluated.
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top