comp.lang.c
[Top] [All Lists]

Re: void * vs. T *

Subject: Re: void * vs. T *
From: santosh
Date: Thu, 27 Mar 2008 14:23:24 +0530
Newsgroups: comp.lang.c

Ark Khasin wrote:

> Is there a way to assert at compile time that T* and void* have
> identical representations, so the build breaks if the assumption is
> invalid? IOW, is there a constant expression for that?

Not as far as I can see. But this can be incorporated in your build
scripts. A small C program can be compiled and executed which can find
this out for you.

> <Background>
> In a particular situation, it makes code more compact and
> comprehensible with a hack of the sort
> Ty myfunc(T *arg)
> {
> //something
> }
> ..............
> //in fact, a const array of structs of those:
> Ty (*pFunc)() = myfunc;
> void *pData = &Sometype_data;
> ..............
> //and the hacky call
> Ty x = pFunc(pData);
> 
> The hack works if T* and void* happen to have identical
> representations. </Backgound>
> 
> E.g.
> (uintmax_t)(void*)123==(uintmax_t)(T*)123 && sizeof(void*)==sizeof(T*)
> seem not to guarantee anything.

No. You should use intptr_t and uintptr_t for this.

> Like, can a malicious implementer bit-invert and reverse the order of
> bits in void* and char* bit patterns, leaving other pointer types
> natural for the platform?

I don't really know, but you'll get an answer from one of the serious
language lawyers, but honestly, though it's good to know about such
stuff, I think you may be wasting your time here. I know of no
platforms where different pointer types are different, let alone
perversions worthy of the DS9k.


<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy