necessary cast?

J

j0mbolar

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

Richard Bos

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
 
T

Thomas Stegen

j0mbolar said:
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.
 
T

Thomas Stegen

Richard said:
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.
 
R

Richard Bos

Thomas Stegen said:
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
 
T

Thomas Stegen

Richard said:
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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top