Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #23973

Re: casting unsigned int to void*

Path csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail
From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: casting unsigned int to void*
Date Mon, 30 Jul 2012 14:14:34 -0400
Organization A noiseless patient Spider
Lines 40
Message-ID <5016CF0A.6020006@verizon.net> (permalink)
References <7edfdd82-a84a-4391-8a63-4a537668d4b7@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
Injection-Info mx04.eternal-september.org; posting-host="d8fef572ea43a1acd556896aa70d63af"; logging-data="19071"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX195AVyj7WSvSyCUP9iYrw8cliytDIsrEFY="
User-Agent Mozilla/5.0 (X11; Linux i686; rv:9.0) Gecko/20111220 Thunderbird/9.0
In-Reply-To <7edfdd82-a84a-4391-8a63-4a537668d4b7@googlegroups.com>
Cancel-Lock sha1:/cgvk3/I0iTvdcjodJsYD6dZCkg=
Xref csiph.com comp.lang.c:23973

Show key headers only | View raw


On 07/30/2012 01:33 PM, Fred K wrote:
> The Xpm pixmap library contains code that casts an unsigned int
> to a (void *), passing that as an argument to a function
> that expects a (void *):
> 
> /* Function prototype: */
> xpmHashIntern (xpmHashTable *table, char *tag, void *data));
> 
> /* Calling it, casting 3rd argument: */
> int a = <something>;
> xpmHashIntern(hashtable, color->string, (void *)a );
> 
> On a 64-bit platform where pointers are 64 bits and ints
> are 32 bits, is this safe?


The fact that pointers are 64 bits and ints are 32 bits makes it more
difficult for this to work, but not impossible. On a platform where both
are the same size, it's more likely to work, but that's still not
guaranteed. The precise requirements are as follows:

Converting an integer constant expression with a value of 0 to void* is
guaranteed by the standard to result in a null pointer value.

Converting a pointer to void into either intptr_t or uintptr_t produces
a value that can be converted back to void*, resulting in a pointer that
is equivalent to the original pointer. Those two types are optional; if
supported, they are defined in <stdint.h>, which was introduced in C99.
You can check whether they are supported by checking whether INTPTR_MAX
or UINTPTR_MAX are #defined.

In all other cases, while converting an integer value to a pointer is
permitted, "the result is implementation-defined, might not be correctly
aligned, might not point to an entity of the referenced type, and might
be a trap representation."

Whether or not this code works correctly is therefore
implementation-specific. It would never have been written and released
unless it actually did work, somewhere, but that's no guarantee that it
will work anywhere else.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

casting unsigned int to void* Fred K <fred.l.kleinschmidt@gmail.com> - 2012-07-30 10:33 -0700
  Re: casting unsigned int to void* Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-30 13:59 -0400
    Re: casting unsigned int to void* falk@rahul.net (Edward A. Falk) - 2012-07-30 19:03 +0000
  Re: casting unsigned int to void* James Kuyper <jameskuyper@verizon.net> - 2012-07-30 14:14 -0400
    Re: casting unsigned int to void* Fred K <fred.l.kleinschmidt@gmail.com> - 2012-07-30 11:23 -0700
      Re: casting unsigned int to void* pacman@kosh.dhis.org (Alan Curry) - 2012-07-30 18:46 +0000
        Re: casting unsigned int to void* Fred K <fred.l.kleinschmidt@gmail.com> - 2012-07-30 12:09 -0700
          Re: casting unsigned int to void* Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-30 21:02 +0100
  Re: casting unsigned int to void* bartek szurgot <basz@no.spam> - 2012-07-30 20:47 +0200
  Re: casting unsigned int to void* "China Blue [Tor], Meersburg" <chine.bleu@yahoo.com> - 2012-07-30 13:04 -0700
    Re: casting unsigned int to void* James Kuyper <jameskuyper@verizon.net> - 2012-07-30 16:22 -0400
      Re: casting unsigned int to void* "China Blue [Tor], Meersburg" <chine.bleu@yahoo.com> - 2012-07-30 14:55 -0700
        Re: casting unsigned int to void* James Kuyper <jameskuyper@verizon.net> - 2012-07-30 18:15 -0400
          Re: casting unsigned int to void* "China Blue [Tor], Meersburg" <chine.bleu@yahoo.com> - 2012-07-30 15:18 -0700
            Re: casting unsigned int to void* Mark Bluemel <mark_bluemel@pobox.com> - 2012-07-31 08:30 +0100
              Re: casting unsigned int to void* "China Blue [Tor], Meersburg" <chine.bleu@yahoo.com> - 2012-07-31 01:17 -0700
    Re: casting unsigned int to void* Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-30 23:36 +0100
    Re: casting unsigned int to void* Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-30 20:45 -0400
      Re: casting unsigned int to void* James Kuyper <jameskuyper@verizon.net> - 2012-07-31 07:23 -0400
  Re: casting unsigned int to void* Keith Thompson <kst-u@mib.org> - 2012-07-30 16:28 -0700
  Re: casting unsigned int to void* James Kuyper <jameskuyper@verizon.net> - 2012-07-31 07:26 -0400
    Re: casting unsigned int to void* 88888 Dihedral <dihedral88888@googlemail.com> - 2012-08-05 09:16 -0700
      Re: casting unsigned int to void* Ike Naar <ike@sdf.lonestar.org> - 2012-08-05 20:50 +0000
        Re: casting unsigned int to void* Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-08-10 11:05 +0300

csiph-web