April 18, 2014
On Thursday, 17 April 2014 at 21:27:44 UTC, Steven Schveighoffer wrote:
> On Thu, 17 Apr 2014 17:04:25 -0400, monarch_dodra <monarchdodra@gmail.com> wrote:
>> void[] will only make sense once you've accepted that "void.sizeof == 1".
>
> It is already accepted that when we talk about length in a void[], it's the number of bytes. But the data has no formal type.

Well, I always thought that "void[] slice" meant "there are slice.length items, starting at slice.ptr. I don't know the size of the individual items".

For example, in C, a lot of functions take "void* first, size_t num, size_t width".

In fact, most of druntime functions take "void[]" buffers that work that way. There's an associated typeid, so that you can now how large each individual items are.

> But any array implicitly casts to void[]. This is why it makes a good parameter for read or write (when reading or writing the binary data).

I guess. I just find it kind of strange that a type "that has no type" would have an actual sizeof. Then again, I thought void had no sizeof in C, but I just checked, and I was wrong.

>> Well, I guess "void[]" is C++'s "char*" for indiscriminate buffers. Speaking of which, does "void*" trigger strict aliasing in D? This subject seems like a hot potato no-one wants to touch.
>
> No, it's equivalent to void *, not char *.
>
> in D, ubyte[] would be the equivalent of C's char *.
>
> -Steve

Correct.
April 18, 2014
On Fri, 18 Apr 2014 02:04:16 -0400, monarch_dodra <monarchdodra@gmail.com> wrote:

> On Thursday, 17 April 2014 at 21:27:44 UTC, Steven Schveighoffer wrote:
>> On Thu, 17 Apr 2014 17:04:25 -0400, monarch_dodra <monarchdodra@gmail.com> wrote:
>>> void[] will only make sense once you've accepted that "void.sizeof == 1".
>>
>> It is already accepted that when we talk about length in a void[], it's the number of bytes. But the data has no formal type.
>
> Well, I always thought that "void[] slice" meant "there are slice.length items, starting at slice.ptr. I don't know the size of the individual items".
>
> For example, in C, a lot of functions take "void* first, size_t num, size_t width".
>
> In fact, most of druntime functions take "void[]" buffers that work that way. There's an associated typeid, so that you can now how large each individual items are.

import std.stdio;

void main()
{
   int[] x = new int[5];
   void[] y = x;
   writeln(y.length); // 20
}

When a function takes a typeid, that usually is because the translation is not made. In druntine cases, the compiler is removing the type, and sticking it into the typeid instead. But the length has not been translated to bytes! It's still in terms of the original type.

In those cases, it's equivalent to:

void[] y = *cast(void[]*)&x;

which would make y.length == 5.

>> But any array implicitly casts to void[]. This is why it makes a good parameter for read or write (when reading or writing the binary data).
>
> I guess. I just find it kind of strange that a type "that has no type" would have an actual sizeof. Then again, I thought void had no sizeof in C, but I just checked, and I was wrong.

It's a little strange, but while void has no size, void[] *does* have a size. The size is in bytes. You can think of an array as "starts at this address, and ends at that address". Because addresses are in terms of bytes, so is the length of that array.

I admit, I didn't think C's void had a size ;) I'm pretty sure it doesn't in D, but then again...

-Steve
April 18, 2014
On Friday, 18 April 2014 at 13:08:04 UTC, Steven Schveighoffer wrote:
> I admit, I didn't think C's void had a size ;) I'm pretty sure it doesn't in D, but then again...
>
> -Steve

Yeah... "static assert(void.sizeof == 1);" passes :/

So in any case, long story short:
"void[]": This is an un-typed buffer, pointing to a memory location that starts at .ptr, and is .length bytes in length.

Also, as far as the GC is concerned, "void" is a type that should be scanned (whether or not the data originally allocated was marked as such is another issue).
April 20, 2014
On 4/18/14, monarch_dodra via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:
> Yeah... "static assert(void.sizeof == 1);" passes :/

Note that you can even have static void arrays. E.g.:

https://issues.dlang.org/show_bug.cgi?id=9691

I'm not sure whether this is an oversight (accepts-invalid) or something else. But it needs to be properly documented.
1 2
Next ›   Last »