Thread overview | |||||
---|---|---|---|---|---|
|
March 12, 2017 I think is a bug? | ||||
---|---|---|---|---|
| ||||
int*[] foo; foo.length = 5; import std.c.string; int* baz = cast(string*)malloc(50); import std.c.stdio; printf("%d %d", foo.length, baz.length ); prints: Error: no property 'length' for type 'int*' BUT: string*[] foo; foo.length = 5; import std.c.string; string* baz = cast(string*)malloc(50); import std.c.stdio; printf("%d %d", foo.length, baz.length ); compiles and prints: 5 -842150451 How come string* suddenly has a .length property? Anyway the result is garbage, so I think this must be a bug. DMD32 D Compiler v2.073.2 |
March 12, 2017 Re: I think is a bug? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Random D user | Random D user wrote:
> How come string* suddenly has a .length property?
due to automatic pointer dereferencing that `.` does. no, not a bug.
|
March 12, 2017 Re: I think is a bug? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Sunday, 12 March 2017 at 01:55:20 UTC, ketmar wrote:
> Random D user wrote:
>
>> How come string* suddenly has a .length property?
>
> due to automatic pointer dereferencing that `.` does. no, not a bug.
Ah... right. Silly me. Of course, since string is actually immutable(char)[].
That's bit of a nasty corner case where -> == . isn't that nice. Fortunately, it's rare.
Thanks.
This happened to me, when I was packing stuff into SoA layout and didn't want to duplicate the length in the struct (implicitly by using []). Of course, I forgot to update one place to use the shared length.
That is:
length
ptr
ptr
ptr
instead of
ptr
length
ptr
length
ptr
length
Perhaps I should do a SoA layout template that somehow disables .length on individual arrays.
|
Copyright © 1999-2021 by the D Language Foundation