Re: Is it conformant to substract two pointer-to-void ?



On 12 mar, 22:36, Eric Sosman <Eric.Sos...@xxxxxxx> wrote:
Francois Grieu wrote:
Why do you *want* this to work?

My application includes the following:

#define CHK_PTR_TYPE(ptr,basetype) (1?(ptr):((ptr)-(basetype*)(ptr))+
(ptr))

and I am concerned that this wont reliably work when basetype is
void or const void

I guess that by "work" you mean "produce a diagnostic if
the type of *ptr is not compatible with basetype?"

Yes, that's my goal.

As written, the test should elicit a diagnostic if the
types are incompatible (which is what I think you want), or
if ptr is void* or if basetype is void (which I think you'd
regard as false positives).

Yes.

I don't understand what the addition at the end is for

It is here to make the expression correct. (ptr)-(basetype*)(ptr)
is a ptrdiff_t and we need to add a ptr to make that
compatible with a ptr, on the left side of the :


If you'd be happy with a slightly different test (compatibility
of ptr with basetype* instead of *ptr with basetype), you might
try

#define CHK_PTR_TYPE(ptr,basetype) \
(1 ? (ptr) : (basetype*)(ptr))

... the idea being that the second and third operands of ?:
must be of compatible pointer types (6.5.15p3), and the
compiler must diagnose a constraint violation.

Indeed that SHOULD do the job. <OT> I'll check with the
various compilers that I target, but I'm a bit pessimistic,
I think I remember one of them is hapy with mixing int and
pointers on each side of : </OT>

Francois Grieu
.



Relevant Pages

  • Re: (void**)&ppval
    ... a sort of "generic data pointer type". ... returned from mallochas type "void *", so that it can point to ... void *tmp; ... T *ptr; ...
    (comp.lang.c)
  • Re: Is it conformant to substract two pointer-to-void ?
    ... and I am concerned that this wont reliably work when basetype is ... void or const void ... if ptr is void* or if basetype is void (which I think you'd ... different test (compatibility of ptr with basetype* instead ...
    (comp.lang.c)
  • Re: warning C4238 : cast reference in void*
    ... void* foo ... why void* ptr = &foo2give this warning? ... Do you understand what rvalue and lvalue mean? ...
    (microsoft.public.vc.language)
  • Re: incrementing void *
    ... void *ptr; ... Which of the following is the correct way to increment the variable ... Its a void * pointer. ... then they want you to treat ptr as if it were a myStruct * ...
    (comp.lang.c)
  • Re: recasting a pointer
    ... Conversion from void * to any other object pointer type is ... In the assignment statement that I commented out, ptr is converted ...
    (comp.lang.c)