Re: Max value of typedef-ed type?



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"
.



Relevant Pages

  • Re: Max value of typedef-ed type?
    ... thing to consider is your reason for wanting to know the maximum ... Python interpreter can talk to. ... The extension should compile on ...
    (comp.lang.c)
  • Re: Max value of typedef-ed type?
    ... I'm writing an extension for Python. ... C source that she compiles onto her machine into a library that the ... anything that supports SysV IPC. ...
    (comp.lang.c)
  • Re: Max value of typedef-ed type?
    ... I'm writing an extension for Python. ... The extension should compile on ... anything that supports SysV IPC. ...
    (comp.lang.c)
  • Re: Max value of typedef-ed type?
    ... Python interpreter can talk to. ... The extension should compile on ... But that's an implementation-specific opinion, ... As for the maximum representable value, if you're willing to restrict ...
    (comp.lang.c)
  • Re: Problem with msvcrt60 vs. msvcr71 vs. strdup/free
    ... Martin v. Löwis wrote: ... > across CRTs in a typical Python extension. ... > to tell from the source code of the extension if such resource ... and since I don't have the official Python 2.4 ...
    (comp.lang.python)