Dereferencing void pointers: why not?

Robert Fraser fraserofthenight at gmail.com
Tue Sep 18 22:36:27 PDT 2007


Almost like dynamic typing... Except instead of being friendly and smiling like a duck it's scary and dangerous like an anteater.

Vladimir Panteleev Wrote:

> The Pascal/Delphi language has a nice feature - it can operate (within limits) on typeless data. This is similar to void pointers, but without the pointer clutter (so is in effect "void references"). Some examples:
> 
> 1) Using a void pointer reference
> 
> Unlike in C/C++/D, you can dereference a void pointer (which in Pascal is represented by the "Pointer" type).
> This allows me to write instead of:
>   PInteger(P)^ := 5;
> this:
>   Integer(P^) := 5;
> 
> The difference in Pascal is more significant since you must define an integer pointer type :)
> In a C-like language, it would be from:
>   *cast(int*)p = 5;
> to
>   cast(int)*p = 5;
> Less asterisks, less strain to the eye :) This may become worth it when the cast expressions grow long and nested.
> 
> 2) Typeless function parameters
> 
> You can write something like this:
>   procedure Test(var Data);
> 
> Any mutable (non-const) value converts implicitely to that "void" type.
> You can also use "const" instead of "var".
> 
> I guess in D it would be declaring such a function argument using "ref void Data", OSLT.
> 
> What can you do with such a reference? Well,
>  - you can cast it to something immediately useable
>  - you can get the address of it (void*)
>  - you can pass it on to other functions
> 
> Thus, it's just a little bit of syntax sugar and flexibility that fits in nicely in the type system. 
> 
> -- 
> Best regards,
>  Vladimir                          mailto:thecybershadow at gmail.com




More information about the Digitalmars-d mailing list