Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > About sizeof

Reply
Thread Tools

About sizeof

 
 
Guest
Posts: n/a
 
      09-11-2003

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

What's the result of iH

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.

--
|
 ___
(-_-)
<>wwwwwwwwwwwwwwww Ŵߪ shepjeng.twbbs.org www
 plum.cs.nccu.edu.tw
 
Reply With Quote
 
 
 
 
Tom Zych
Guest
Posts: n/a
 
      09-11-2003
Someone whose name my newsreader would probably mangle wrote:

> 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.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo '(E-Mail Removed)' | rot13
 
Reply With Quote
 
 
 
 
Dan Pop
Guest
Posts: n/a
 
      09-11-2003
In <48EQ70$(E-Mail Removed)> http://www.velocityreviews.com/forums/(E-Mail Removed) () writes:


> int i = 0;
> i = sizeof( i++ );
>
> What's the result of iH
>
> 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
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
Jirka Klaue
Guest
Posts: n/a
 
      09-11-2003
Tom Zych wrote:
> Someone whose name my newsreader would probably mangle wrote:
>> 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

 
Reply With Quote
 
=?ISO-8859-1?Q?Johan_Aur=E9r?=
Guest
Posts: n/a
 
      09-11-2003
On Thu, 11 Sep 2003, Tom Zych wrote:

> Someone whose name my newsreader would probably mangle wrote:
>
> > 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 ).

--
(E-Mail Removed)


 
Reply With Quote
 
Jeff
Guest
Posts: n/a
 
      09-11-2003

"" <(E-Mail Removed)> wrote in message
news:48EQ70$(E-Mail Removed)...
>
> int i = 0;
> i = sizeof( i++ );
>
> What's the result of iH


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--));


--
Jeff
-je6543 at yahoo.com


 
Reply With Quote
 
Jirka Klaue
Guest
Posts: n/a
 
      09-11-2003
Johan Aurr 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

 
Reply With Quote
 
Tom Zych
Guest
Posts: n/a
 
      09-11-2003
Jirka Klaue wrote:

> No, sizeof does not evaluate i++.


Oh. Ok, my bad.

Have to remember that sizeof(i++) for the IOCCC. It's no good for
anything else

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo '(E-Mail Removed)' | rot13
 
Reply With Quote
 
Derk Gwen
Guest
Posts: n/a
 
      09-11-2003
# 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?

--
Derk Gwen http://derkgwen.250free.com/html/index.html
But I do believe in this.
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      09-12-2003
On Thu, 11 Sep 2003 19:14:34 +0200, Jirka Klaue
<(E-Mail Removed)-berlin.de> wrote in comp.lang.c:

> Johan Aurr 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


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sizeof(EmptyStruct) in C and C++ (was: Base {}; sizeof(Base) == 1?) Alex Vinokur C Programming 7 08-14-2006 04:57 PM
sizeof( int ) != sizeof( void * ) blufox C Programming 2 05-22-2006 03:25 PM
#define ARR_SIZE sizeof(arr)/sizeof(arr[0]) Vinu C Programming 13 05-12-2005 06:00 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments