RE: [PATCH v2 1/2] system_data_types.7: Add 'void *'

From: David Laight
Date: Fri Oct 02 2020 - 05:10:37 EST


From: Alejandro Colomar
> Sent: 02 October 2020 09:25
> > For 'void *' you should also mention that one cannot use arithmetic on
> > void * pointers, so they're special in that way too.
>
> Good suggestion!

Except that is a gcc extension that is allowed in the kernel.

> > Also, you should
> > warn that because one can convert from any pointer type to void * and
> > then to any other pointer type, it's a deliberate hole in C's
> > type-checking.
>
> Also good. I'll talk about generic function parameters for this.

That isn't what the C standard says at all.
What is says is that you can cast any data pointer to 'void *'
and then cast it back to the same type.

This matters because the compiler will 'remember' structure
alignment through 'void *' casts.
So you can't use memcpy() to copy from a potentially misaligned
(typed) pointer.

'void *' should only be used for structures that are 'a sequence of bytes'.
(eg things that look a bit like read() or write()).

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)