About sizeof

³

³á³á³á³á

int i = 0;
i = sizeof( i++ );

What's the result of i¡H

I think the result may be sizeof( int ) or sizeof( int ) + 1, or it is

undefined behavior.

I know that i = i++ is undefined behavior,

but sizeof is calculated at compiling time.

--
|
 ___
(-_-)
<¡ä¡ä>¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w ªÅ´ßªª³õ shepjeng.twbbs.org ¢w¢w¢w
¡þ plum.cs.nccu.edu.tw
 
T

Tom Zych

Someone said:
int i = 0;
i = sizeof( i++ );
I think the result may be sizeof( int ) or sizeof( int ) + 1, or it is
undefined behavior.
I know that i = i++ is undefined behavior,
but sizeof is calculated at compiling time.

But you're still changing the value of i twice in one statement[1].
i++ increments it. i = sizeof... sets it to a constant value.
Undefined behavior. Anything can happen. The computer can whistle
"Night in Tunisia", make a pot of coffee, and explode.

This ignores the question of whether sizeof (i++) is legal or
well-defined. I don't know that one.

[1] To be precise, without an intervening sequence point.
 
D

Dan Pop

int i = 0;
i = sizeof( i++ );

What's the result of i¡H

I think the result may be sizeof( int ) or sizeof( int ) + 1, or it is
undefined behavior.

It's sizeof(int). When the argument of sizeof is an expression (other
than a C99 VLA), it is NOT evaluated, the compiler merely determines its
type and produces the appropriate result.
I know that i = i++ is undefined behavior,

This is a completely different issue.
but sizeof is calculated at compiling time.

It need not be, unless used in a context where a constant expression is
required.

Dan
 
J

Jirka Klaue

Tom said:
Someone said:
int i = 0;
i = sizeof( i++ );
I think the result may be sizeof( int ) or sizeof( int ) + 1, or it is
undefined behavior.
I know that i = i++ is undefined behavior,
but sizeof is calculated at compiling time.

But you're still changing the value of i twice in one statement[1].
i++ increments it. i = sizeof... sets it to a constant value.
Undefined behavior. Anything can happen. The computer can whistle
"Night in Tunisia", make a pot of coffee, and explode.

No, sizeof does not evaluate i++.

6.5.3.4 The sizeof operator
Semantics
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 ofthe 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 an integer constant.
This ignores the question of whether sizeof (i++) is legal or
well-defined. I don't know that one.

The type of i++ is int, therefore it's equivalent to sizeof(int).

Jirka
 
?

=?ISO-8859-1?Q?Johan_Aur=E9r?=

Someone said:
int i = 0;
i = sizeof( i++ );
I think the result may be sizeof( int ) or sizeof( int ) + 1, or it is
undefined behavior.
I know that i = i++ is undefined behavior,
but sizeof is calculated at compiling time.

But you're still changing the value of i twice in one statement[1].
i++ increments it. i = sizeof... sets it to a constant value.

No, sizeof does not evaluate its operand, so sizeof( i++ ) is equivalent
to sizeof( i ).
 
J

Jeff

³á³á³á³á said:
int i = 0;
i = sizeof( i++ );

What's the result of i¡H

Using sizeof to "i++ " is similar to "sizeof(int)". i++ returns an integer,
so your compiler just think about "what is the size of integer in this
implementation"

try this

printf("The size is %d \n", sizeof(i*32/9 + 2525 - i++ * i--));
 
J

Jirka Klaue

Johan Aurér wrote:
....
No, sizeof does not evaluate its operand, so sizeof( i++ ) is equivalent
to sizeof( i ).

That's true in this case, but not generally:

short i;
assert( sizeof(i) == sizeof (i + 1) );

Jirka
 
D

Derk Gwen

# int i = 0;
# i = sizeof( i++ );
#
# What's the result of i¡H

'Doctor! Doctor! It hurts when I do this!'
'Then don't that.'

Is this like a review of obfustucated C contest code, or exams of a sadistic
instructor, or malevolent job interviewers? I see people bring up truly twisted
code that no programmer would ever use in production code, and then ask
'what does this do?' I'm left wonderring, why do this people even want to know?
 
J

Jack Klein

Johan Aurér wrote:
...

That's true in this case, but not generally:

short i;
assert( sizeof(i) == sizeof (i + 1) );

Jirka

Works just fine on the TI DSP compiler I'm working with at the moment.

If fact this one will not assert either, on this particular
implementation:

char i;
assert (sizeof(i) == sizeof(i + 1));

CHAR_BIT is 16 and char, short, and int all have identical
representations.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
M

Mantorok Redgormor

Jirka Klaue said:
Johan Aurér wrote:
...

That's true in this case, but not generally:

short i;
assert( sizeof(i) == sizeof (i + 1) );

Jirka

Why does sizeof i + 1 give 3 and not the size of an int on my system?
I thought it does not evaluate its operand? This makes no sense.
 
T

Tom Zych

Mantorok said:
Why does sizeof i + 1 give 3 and not the size of an int on my system?
I thought it does not evaluate its operand? This makes no sense.

Because sizeof has a higher precedence than +. Your expression is
equivalent to (sizeof i) + 1.
 
I

Irrwahn Grausewitz

Why does sizeof i + 1 give 3 and not the size of an int on my system?
I thought it does not evaluate its operand? This makes no sense.

Hehehe, you calculated: ( sizeof i ) + 1 , which gives a result
quite different from: sizeof ( i + 1 ).

Caught by operator precedence... :)

Irrwahn
 
J

Jeff

Derk Gwen said:
# int i = 0;
# i = sizeof( i++ );
#
# What's the result of i¡H

'Doctor! Doctor! It hurts when I do this!'
'Then don't that.'

Is this like a review of obfustucated C contest code, or exams of a sadistic
instructor, or malevolent job interviewers? I see people bring up truly twisted
code that no programmer would ever use in production code, and then ask
'what does this do?' I'm left wonderring, why do this people even want to know?

When one day someone write such kind of code, and you have to understand it
or explain it to your children :)
 
J

Jirka Klaue

Jack said:
Works just fine on the TI DSP compiler I'm working with at the moment.

If fact this one will not assert either, on this particular
implementation:

char i;
assert (sizeof(i) == sizeof(i + 1));

CHAR_BIT is 16 and char, short, and int all have identical
representations.

That's nice. This compiler isn't a free-standing implementation, is it?
Otherwise EOF is going to be a serious problem.
And I wouldn't call it "general".

Jirka
 
J

Jirka Klaue

Jirka said:
Jack Klein wrote: ....

That's nice. This compiler isn't a free-standing implementation, is it?
s/free-standing/hosted/

Otherwise EOF is going to be a serious problem.
And I wouldn't call it "general".

Jirka
 
D

Dan Pop

In said:
That's nice. This compiler isn't a free-standing implementation, is it?

Of course it is. I doubt anyone would attempt a hosted implementation
with UCHAR_MAX > INT_MAX and/or a general purpose computer with a DSP
as its main CPU.
Otherwise EOF is going to be a serious problem.

Because the standard C library specification is written with the implicit
assumption that UCHAR_MAX <= INT_MAX. I've never understood why they
didn't make it explicit.

Dan
 
A

Alex

Why does sizeof i + 1 give 3 and not the size of an int on my system?
I thought it does not evaluate its operand? This makes no sense.

This makes absolute sense.

sizeof i + 1 == (sizeof i) + 1

....which is quite different from:

sizeof (i + 1)

Alex
 

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

Similar Threads


Members online

Forum statistics

Threads
473,813
Messages
2,569,699
Members
45,489
Latest member
SwethaJ

Latest Threads

Top