473,404 Members | 2,137 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,404 software developers and data experts.

Casting return value to fit within constraints of error values (portability)

I realize this may work for the majority of platforms. But I am
concerned about the aspects of sign extension and portability.

Is this portable:

{
unsigned long bw;
[...]

if ((signed long)(bw = read(s, buf, sizeof(buf))) == -1) {
error_handling();
}
}

With the goal being able to utilize larger unsigned values but still
being able to check for a return value of "-1" (since this is the
mechanism read() uses) to handle errors?

Jan 28 '06 #1
4 1745
clayne wrote:
unsigned long bw;
[...]

if ((signed long)(bw = read(s, buf, sizeof(buf))) == -1) {


In retrospect this is probably a pointless "workaround" as read() won't
ever be returning more than signed size_t (ssize_t) in the first place,
so no need to declare 'bw' as unsigned long or size_t, etc.

Jan 28 '06 #2
clayne wrote:
In retrospect this is probably a pointless "workaround" as read() won't
ever be returning more than signed size_t (ssize_t) in the first place,


Interesting though that I already see (Solaris for instance):

ssize_t read(int fildes, void *buf, size_t nbyte);

The read() function attempts to read nbyte bytes from the
file associated with the open file descriptor, fildes, into
the buffer pointed to by buf.

Jan 28 '06 #3
On 27 Jan 2006 20:57:31 -0800, in comp.lang.c , "clayne"
<cl****@anodized.com> wrote:
ssize_t read(int fildes, void *buf, size_t nbyte);


you /are/ aware that read() is offtopic here? Its a posix function,
probably best discussed in a unix group.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jan 28 '06 #4
On 27 Jan 2006 20:40:48 -0800, "clayne" <cl****@anodized.com> wrote in
comp.lang.c:

I wonder whether it is worth replying, since you have done a
commendable job of replying to yourself.

And just for the heck of it, I will point out that there is no "read"
function in C, it is a POSIX extension and hence off-topic here. But
your underlying problem is similar to one that could happen when you
call time(), which could return (time_t)-1.
I realize this may work for the majority of platforms. But I am
concerned about the aspects of sign extension and portability.

Is this portable:

{
unsigned long bw;
[...]

if ((signed long)(bw = read(s, buf, sizeof(buf))) == -1) {
Why, oh why, would you want to do this?
error_handling();
}
}

With the goal being able to utilize larger unsigned values but still
being able to check for a return value of "-1" (since this is the
mechanism read() uses) to handle errors?


Let's replace read with time(), to keep it topical.

#include <time.h>

void func(void)
{
time_t now;
if ((now = time(NULL)) == (time_t)-1)
{
/* error handling */
}
}

Don't case the returned value to another type, cast the comparison
value constant to the type of the return value.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jan 28 '06 #5

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

Similar topics

5
by: Derek Cooper | last post by:
I hope you can help me. I posted this in the microsoft sql server newsgroup a few days ago and got no response so I thought I'd try here. If I can provide any clarification I'll be glad to do so....
1
by: dbuchanan | last post by:
VB.NET 2003 / SQLS2K The Stored Procedure returns records within Query Analyzer. But when the Stored Procedure is called by ADO.NET ~ it produced the following error message. ...
5
by: Tom Carroll | last post by:
When assigning or testing equality between a type and a constant, should the constant be casted to the type? I.e. should fork() == -1 be written as fork() == (pid_t)-1? should uid = 45 be...
8
by: David M. Wilson | last post by:
I know this doesn't specifically pertain to the scope of comp.lang.c, but I don't know of a better forum to post it on, besides, what is the practicality of comp.lang.c if it weren't for answers to...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
14
by: mr_semantics | last post by:
I have been reading about the practise of casting values to unsigned char while using the <ctype.h> functions. For example, c = toupper ((unsigned char) c); Now I understand that the standard...
8
by: wkaras | last post by:
In my compiler, the following code generates an error: union U { int i; double d; }; U u; int *ip = &u.i; U *up = static_cast<U *>(ip); // error I have to change the cast to...
46
by: Bill Cunningham | last post by:
I have heard that return -1 is not portable. So what would be the answer to portability in this case? What about exit(0) and exit (-1) or exit (1)? Or would it be best to stick with C's macros,...
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.