www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - I think is a bug?

reply Random D user <no email.com> writes:
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
Mar 11 2017
parent reply ketmar <ketmar ketmar.no-ip.org> writes:
Random D user wrote:

 How come string* suddenly has a .length property?
due to automatic pointer dereferencing that `.` does. no, not a bug.
Mar 11 2017
parent Random D user <no email.com> writes:
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.
Mar 11 2017