Pointer to string allows assigning int[]?

simendsjo simendsjo at gmail.com
Mon Nov 12 13:08:38 PST 2012


On Monday, 12 November 2012 at 21:03:51 UTC, cal wrote:
> Is the following a bug?
>
> import std.c.string, core.memory;
>
> void* makeNew(T)()
> {
>     T t = T.init;
>     void* ptr = GC.malloc(T.sizeof);
>     memcpy(ptr, &t, T.sizeof);
>     return ptr;
> }
>
> void main()
> {
>     alias string T;
>     T* iptr = cast(T*)makeNew!(T);
>     (*iptr) = [1,2,3]; // this is allowed
> }

It's not a bug.

string is an alias for immutable(char)[].
ubyte can be implicitly converted to char.
All your numbers are less than 256, so dmd is able to convert 
them to char.
If you try this, it fails.
string s = [256]; // 256 is > char.max



More information about the Digitalmars-d-learn mailing list