necessary cast?

Discussion in 'C Programming' started by j0mbolar, Apr 28, 2004.

  1. j0mbolar

    j0mbolar Guest

    I was looking over some code and came across this:

    new_time = mktime(&tm);

    if(new_time == (time_t)-1)
    goto err;


    is this cast necessary in a strictly conforming
    program?
    j0mbolar, Apr 28, 2004
    #1
    1. Advertising

  2. j0mbolar

    Richard Bos Guest

    (j0mbolar) wrote:

    > I was looking over some code and came across this:
    >
    > new_time = mktime(&tm);
    >
    > if(new_time == (time_t)-1)
    > goto err;
    >
    > is this cast necessary in a strictly conforming
    > program?


    I think so. If time_t is a small type, the comparison (new_time == -1)
    will result in new_time being promoted to int, and then compared to -1;
    if time_t is also unsigned, this comparison will be false even if
    new_time is actually equal to (time_t)-1.
    For this to happen, time_t must be unsigned short, unsigned char, or a
    small C99 unsigned type, and the range of that type must be smaller than
    that of unsigned int. This is, of course, not very likely, but when
    you're after strict conformance, "unlikely but legal" is bad enough.

    Richard
    Richard Bos, Apr 28, 2004
    #2
    1. Advertising

  3. j0mbolar wrote:
    > I was looking over some code and came across this:
    >
    > new_time = mktime(&tm);
    >
    > if(new_time == (time_t)-1)
    > goto err;
    >
    >
    > is this cast necessary in a strictly conforming
    > program?


    No cast can make this a strictly conforming program.
    The only guarantee given about time_t is that it is an
    arithmetic type. This means it can be a float or a double,
    signed or unsigned integer type. The above seems to make the
    assumption that time_t is an unsigned integer type. This
    is relying on implementation defined behaviour and so the
    program is not strictly conforming.

    --
    Thomas.
    Thomas Stegen, Apr 28, 2004
    #3
  4. Richard Bos wrote:

    > (j0mbolar) wrote:
    >
    >
    >>I was looking over some code and came across this:
    >>
    >>new_time = mktime(&tm);
    >>
    >>if(new_time == (time_t)-1)
    >> goto err;
    >>
    >>is this cast necessary in a strictly conforming
    >>program?

    >
    >
    > I think so.


    The above will not make the program strictly conforming as time_t
    is an arithmetic type. Which means it can be any integer type or any
    floating point type.

    So even though the above is never invalid C its behaviour does
    depend on the actual type of time_t.

    --
    Thomas.
    Thomas Stegen, Apr 28, 2004
    #4
  5. j0mbolar

    Richard Bos Guest

    Thomas Stegen <> wrote:

    > Richard Bos wrote:
    >
    > > (j0mbolar) wrote:
    > >
    > >>I was looking over some code and came across this:
    > >>
    > >>new_time = mktime(&tm);
    > >>
    > >>if(new_time == (time_t)-1)
    > >> goto err;
    > >>
    > >>is this cast necessary in a strictly conforming
    > >>program?

    > >
    > > I think so.

    >
    > The above will not make the program strictly conforming as time_t
    > is an arithmetic type. Which means it can be any integer type or any
    > floating point type.


    Even so, (time_t)-1 must be the value returned by mktime() on error:

    # [#4] The mktime function returns the specified calendar time
    # encoded as a value of type time_t. If the calendar time
    # cannot be represented, the function returns the value
    # (time_t)-1. [From n869.txt, 7.23.2.3.]

    The example following this excerpt even uses that very comparison.

    Richard
    Richard Bos, Apr 28, 2004
    #5
  6. Richard Bos wrote:
    >
    > Even so, (time_t)-1 must be the value returned by mktime() on error:
    >
    > # [#4] The mktime function returns the specified calendar time
    > # encoded as a value of type time_t. If the calendar time
    > # cannot be represented, the function returns the value
    > # (time_t)-1. [From n869.txt, 7.23.2.3.]
    >
    > The example following this excerpt even uses that very comparison.


    Good to know, cheers.

    --
    Thomas.
    Thomas Stegen, Apr 28, 2004
    #6
    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. Replies:
    1
    Views:
    568
  2. LaEisem

    Is a "cast" ever necessary in "return"?

    LaEisem, Oct 28, 2003, in forum: C Programming
    Replies:
    10
    Views:
    617
  3. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,063
    Dan Pop
    Feb 10, 2004
  4. EvilRix
    Replies:
    8
    Views:
    624
    Martin Dickopp
    Feb 14, 2004
  5. Pavel
    Replies:
    7
    Views:
    515
    Pavel
    Sep 19, 2010
Loading...

Share This Page