Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > necessary cast?

Reply
Thread Tools

necessary cast?

 
 
j0mbolar
Guest
Posts: n/a
 
      04-28-2004
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?
 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      04-28-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
 
 
 
Thomas Stegen
Guest
Posts: n/a
 
      04-28-2004
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.

 
Reply With Quote
 
Thomas Stegen
Guest
Posts: n/a
 
      04-28-2004
Richard Bos wrote:

> (E-Mail Removed) (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.

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      04-28-2004
Thomas Stegen <(E-Mail Removed)> wrote:

> Richard Bos wrote:
>
> > (E-Mail Removed) (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
 
Reply With Quote
 
Thomas Stegen
Guest
Posts: n/a
 
      04-28-2004
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.

 
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
is a digital filter necessary? nesppi@gmail.com VHDL 1 01-13-2006 11:37 AM
Book is necessary to pass OCP certification. Oneil Microsoft Certification 0 11-27-2005 07:20 AM
cookie handling - is there a list of *necessary* cookies ?? cookie-monster Firefox 1 03-05-2005 03:40 AM
Is an access-group command necessary for ACLs? ed@novani.com Cisco 4 12-30-2004 05:27 PM
ORM resources necessary for 70-300 TomTom MCSD 7 08-17-2004 07:08 AM



Advertisments