473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

necessary cast?

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?
Nov 14 '05 #1
5 1164
j0******@engineer.com (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
Nov 14 '05 #2
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.

Nov 14 '05 #3
Richard Bos wrote:
j0******@engineer.com (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.

Nov 14 '05 #4
Thomas Stegen <ts*****@cis.strath.ac.uk> wrote:
Richard Bos wrote:
j0******@engineer.com (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
Nov 14 '05 #5
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.

Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: alg | last post by:
Why is so necessary sometimes in C++ that you have to cast a pointer between a base object and a derived object? Could you give me some examples for me to understand this concept deeply? Thanks...
13
by: GianGuz | last post by:
Everyone knows about the complex and cpu-expensive procedures taken by dynamic_cast to find the right function call in a virtual classes hierarchy. The question I would to rise is when...
0
by: Aaron W. West | last post by:
Fun with CAST! (Optimized SQLServerCentral script posts) I found some interesting "tricks" to convert binary to hexadecimal and back, which allow doing 4 or 8 at a time. Test code first: --...
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
5
by: Frederick Gotham | last post by:
Before I begin, here's a list of assumptions for this particular example: (1) unsigned int has no padding bits, and therefore no invalid bit- patterns or trap representations. (2) All types have...
46
by: ajba74 | last post by:
Hi fellows, I am reading some books to learn the C programming language, and sometimes I have the feeling that when somebody becomes a C expert, he must learn a more modern and object-oriented...
28
by: lovecreatesbeauty | last post by:
Is the keyword const necessary in the comparison function in qsort and bsearch? int (*compar)(const void *, const void *) If the pointer cannot be dereferenced why worry if the pointed object...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.