-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 08/30/2012 06:18 PM, Jens Gustedt wrote:
> Am 30.08.2012 19:49, schrieb Garrett Hartshaw:
>> 'char *' says "this is a pointer to an object of type 'char'",
>> 'unsigned char *' says "this is a pointer to an object of type
>> 'unsigned char'", 'void *' says "this is a pointer to an object
>> of an unknown/any type".
>
> no, at least for C "void" is a type, too.
>
> The void type comprises an empty set of values; it is an
> incomplete object type that cannot be completed.
>
> so "void*" says pointer to void. This is not "nothing" or
> "unknown", in that sense void is not much different from "struct
> nix" when you never actually define "struct nix" somewhere. It
> behaves a bit different in terms of implicit conversions, sure, but
> the sematic is the same.
>
>> If you want to interpret an object as a sequence of bytes, then
>> use 'unsigned char *' or 'uint8_t *'. I see 'unsigned char' as
>> meaning a character in an encoding that is represented as an
>> 8-bit unsigned integer, while 'uint8_t' is an arbitrary 8-bit
>> unsigned integer. (e.g. I would write "unsigned char a = 'a'" and
>> "uint8_t b = 10", but not "unsigned char c = 10" or "uint8_t d =
>> 'd'")
>>
>> If on the other hand you wish to have an opaque pointer to an
>> object of unknown type, or to an uninitialized memory region,
>> then use 'void *'.
>
> A "void*" pointer is not "an uninitialized" memory region. It may
> well already be initialized.
As Keith said, that was meant to be how it should be used, rather than
exactly what it means to the compiler.
>
> But I am really surprised about this strong need of opaque pointer
> in C++. If we follow the basic ideas of C++ such a usage should be
> very rare in C++, no? Isn't that what "abstract base classes" have
> been invented for?
>
>> Even if 'void *' was redefined to mean 'unsigned char *', I
>> would still distinguish between their uses just as I distinguish
>> between 'unsigned char' and 'uint8_t'.
>
> Therefore I had my question that came afterward and that you seem
> to have overlooked:
>
>>> Would you be more comfortable if the standard introduced a
>>> type alias "byte" for "unsigned char"?
>>
>>> That would completely be conceivable. For example C11
>>> introduces
>>
>>> typedef int errno_t;
>>
>>> but by reclaiming a different semantic for "errno_t" than for
>>> "int".
>
> The naming of "char" (and the two others) certainly doesn't cover
> all its uses that these type nowadays have, and it would be good to
> find semantic names for the different use cases.
>
> typedef char byte_t; // for the minimal addressable
> storage unit typedef unsigned char bitX_t; // for an arbitrary
> collection of unspecific data, usually X == 8 typedef unsigned char
> uintX_t; // for use as small unsigned integer, usually X == 8
> typedef signed char intX_t; // for use as small signed
> integer, usually X == 8
>
> Jens
>
One of the major uses of typedef is to allow this kind of
disambiguation between types that are represented identically. While I
don't often have need to work low-level directly on bytes, if I did I
would not hesitate to create a byte_t typedef, and if it was already
included in the standard, so much the better (although I would
probably typedef it as 'unsigned char' to avoid the
implementation-defined signed-ness of plain 'char').
However, even if 'byte_t' were added to the standard, I would still
want to distinguish between 'byte_t *' (used as a pointer to a byte)
and 'void *' (used as an opaque pointer or a pointer to uninitialized
memory) *even if they both meant the same thing to the compiler*.
Granted it would better for 'memcpy' and friends to use 'byte_t *'
rather than 'void *', as their underlying functionality is specified
in terms of moving/copying bytes. 'void *' would still be required *in
C* as the return value of 'malloc' (unitialized memory) and for use as
a userData parameter in callbacks (opaque pointer to an object of
unspecified type). There is little use for 'void *' in pure C++ code,
but it would still be required in the language to interface with C code.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)
Comment: Using GnuPG with Mozilla -
http://enigmail.mozdev.org/
iQEcBAEBAgAGBQJQQBxQAAoJEP8JewLaHvzSQJ0H+gNUnCggPW CdrovjjEDnANY0
mL2/5I+R+Ckn7sMYDWmdc6jhxQPcfoKX7aHT9C2DV1MWRfigMb4z0r sKqcc+WjUY
FJXEjLtlv9H+IHHuypXWNqVAPm7HgpXq74L/yhv2H2IlJqra6DW58ULdWRm6DUSN
xHijMzGpuNIw7LE3zzilinJfvP26+pxXZDuFmWXs2pCAPXgikZ S1AG9PQWnVWXLz
DXDZ7nejxIc8FIesNfxO3NLzzMVZ69ZhyfB4M6UwCD2TdeRdrA Yay5f3sEHKtIvG
N2RXXXV+J36ZX4w0i6dQuuCuAHzBaPwBcvtW79nIzgAv5pqlCD A4kuMl8WF1Y4s=
=SUbB
-----END PGP SIGNATURE-----