About sizeof

Discussion in 'C Programming' started by ³á³á³á³á, Sep 11, 2003.

  1. 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
     
    ³á³á³á³á, Sep 11, 2003
    #1
    1. Advertising

  2. ³á³á³á³á

    Tom Zych Guest

    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 '' | rot13
     
    Tom Zych, Sep 11, 2003
    #2
    1. Advertising

  3. ³á³á³á³á

    Dan Pop Guest

    In <48EQ70$> (³á³á³á³á) writes:


    > 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
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Sep 11, 2003
    #3
  4. ³á³á³á³á

    Jirka Klaue Guest

    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
     
    Jirka Klaue, Sep 11, 2003
    #4
  5. 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 ).

    --
     
    =?ISO-8859-1?Q?Johan_Aur=E9r?=, Sep 11, 2003
    #5
  6. ³á³á³á³á

    Jeff Guest

    "³á³á³á³á" <> wrote in message
    news:48EQ70$...
    >
    > 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--));


    --
    Jeff
    -je6543 at yahoo.com
     
    Jeff, Sep 11, 2003
    #6
  7. ³á³á³á³á

    Jirka Klaue Guest

    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
     
    Jirka Klaue, Sep 11, 2003
    #7
  8. ³á³á³á³á

    Tom Zych Guest

    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 '' | rot13
     
    Tom Zych, Sep 11, 2003
    #8
  9. ³á³á³á³á

    Derk Gwen Guest

    # 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.
     
    Derk Gwen, Sep 11, 2003
    #9
  10. ³á³á³á³á

    Jack Klein Guest

    On Thu, 11 Sep 2003 19:14:34 +0200, Jirka Klaue
    <-berlin.de> wrote in comp.lang.c:

    > 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


    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
     
    Jack Klein, Sep 12, 2003
    #10
  11. Jirka Klaue <-berlin.de> wrote in message news:<-berlin.de>...
    > 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


    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.
     
    Mantorok Redgormor, Sep 12, 2003
    #11
  12. ³á³á³á³á

    Tom Zych Guest

    Mantorok Redgormor wrote:

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

    --
    Tom Zych
    This email address will expire at some point to thwart spammers.
    Permanent address: echo '' | rot13
     
    Tom Zych, Sep 12, 2003
    #12
  13. (Mantorok Redgormor) wrote:

    >Jirka Klaue <-berlin.de> wrote in message news:<-berlin.de>...
    >> 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

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

    --
    I can't see it from here, but it looks good to me.
     
    Irrwahn Grausewitz, Sep 12, 2003
    #13
  14. ³á³á³á³á

    Jeff Guest

    "Derk Gwen" <> wrote in message
    news:...
    > # 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 :)

    --
    Jeff
    -je6543 at yahoo.com
     
    Jeff, Sep 12, 2003
    #14
  15. ³á³á³á³á

    Jirka Klaue Guest

    Jack Klein wrote:
    >Jirka Klaue wrote:
    >>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) );

    >
    > 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
     
    Jirka Klaue, Sep 12, 2003
    #15
  16. ³á³á³á³á

    Jirka Klaue Guest

    Jirka Klaue wrote:
    > Jack Klein wrote:

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


    s/free-standing/hosted/

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


    Jirka
     
    Jirka Klaue, Sep 12, 2003
    #16
  17. ³á³á³á³á

    Dan Pop Guest

    In <bjs5mn$ldq$-Berlin.DE> Jirka Klaue <-berlin.de> writes:

    >Jack Klein wrote:
    >>
    >> 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?


    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
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Sep 12, 2003
    #17
  18. ³á³á³á³á

    Alex Guest

    Mantorok Redgormor <> wrote:
    > Jirka Klaue <-berlin.de> wrote in message news:<-berlin.de>...
    >> Johan Aurer 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


    > 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
     
    Alex, Sep 12, 2003
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek
    Replies:
    7
    Views:
    24,421
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    664
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,506
    Lawrence Kirby
    May 12, 2005
  4. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    580
    Joe Smith
    May 22, 2006
  5. Alex Vinokur
    Replies:
    7
    Views:
    515
    Clark S. Cox III
    Aug 14, 2006
Loading...

Share This Page