Re: Max value of typedef-ed type?
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Mon, 15 Dec 2008 13:34:43 -0800
Nikita The Spider <NikitaTheSpider@xxxxxxxxx> writes:
On Dec 15, 11:57 am, Eric Sosman <Eric.Sos...@xxxxxxx> wrote:[...]
Another
thing to consider is your reason for wanting to know the maximum
value a key_t can hold: What are you going to do with that value
once you have it, and can your problem be rearranged so you don't
need the information? (Sometimes it can't, but it never hurts to
ask.)
A fair question. I'm writing (in C) an extension for Python. The
extension will be downloaded by the end-user (a Python programmer) as
C source that she compiles onto her machine into a library that the
Python interpreter can talk to. The extension should compile on
anything that supports SysV IPC. (I've got a similar extension for
POSIX IPC.) It's incumbent upon me as the extension developer to
expose to Python the limits the Python caller must obey, so if she
passes a value of, say, 999999 for a key I can raise a "Sorry that's
too big" error.
I'd like to know the maximum (or minimum) of uid_t, gid_t, mode_t and
key_t. I'd also like to know the maximum value that a semaphore can
hold. It's #defined as SEMVMX on some systems, but on others it is
configurable and not in a header file at all (which is a different
problem entirely that may require multiple daiquiri to solve). I'd at
least like to restrict it to the maximum that the type can hold.
I think what you're looking for is the maximum *meaningful* value of
these types rather than the maximum representable value, and that's
not really a C question.
As for the maximum representable value, if you're willing to restrict
the set of systems on which this will work, you might get away with
something like this:
switch (sizeof (key_t) * CHAR_BIT) {
case 8: return 0x7f;
case 16: return 0x7fff;
case 32: return 0x7fffffff;
case 64: return 0x7fffffffffffffff;
default: /* fatal error */
}
(Note the phrase "something like"; I haven't tested this, and I don't
guarantee that it's correct.)
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- Follow-Ups:
- Re: Max value of typedef-ed type?
- From: Nikita The Spider
- Re: Max value of typedef-ed type?
- References:
- Max value of typedef-ed type?
- From: Nikita The Spider
- Re: Max value of typedef-ed type?
- From: Eric Sosman
- Re: Max value of typedef-ed type?
- From: Nikita The Spider
- Max value of typedef-ed type?
- Prev by Date: Re: Animosity on the part of Mr Richard Heathfield
- Next by Date: Re: Max value of typedef-ed type?
- Previous by thread: Re: Max value of typedef-ed type?
- Next by thread: Re: Max value of typedef-ed type?
- Index(es):
Relevant Pages
|