Bug 22723 – Cannot allocate a slice on heap

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-02-01T17:36:15Z
Last change time
2024-12-13T19:20:44Z
Assigned to
No Owner
Creator
Temtaime
Moved to GitHub: dmd#18082 →

Comments

Comment #0 by temtaime — 2022-02-01T17:36:15Z
This code should be allowed, but it results in error: ubyte[]* a = new ubyte[]; Error: missing length argument for array The only way to create it i found so far is: ubyte[]* a = &(new ubyte[][1])[0];
Comment #1 by moonlightsentinel — 2022-02-01T21:20:22Z
Allocating slices by themselves is explicitly disallowed due to the ambiguitee possibilitly IIUC (you almost always want to allocate the actual array). run.dlang.io shows the degradation of the error message: Up to 2.078.1: Failure with output: onlineapp.d(3): Error: new can only create structs, dynamic arrays or class objects, not ubyte[]'s 2.079.1 to 2.090.1: Failure with output: onlineapp.d(3): Error: new can only create structs, dynamic arrays or class objects, not `ubyte[]`'s 2.091.1 to 2.093.1: Failure with output: onlineapp.d(3): Error: cannot create a `ubyte[]` with `new` Since 2.094.1: Failure with output: onlineapp.d(3): Error: missing length argument for array The error message probably complain about the type + suggest adding a length (which should be the solution in most cases)
Comment #2 by b2.temp — 2022-02-02T07:43:32Z
Isn't the issue more like a request to have a way to allocate the slice payload on the heap ? Maybe a druntime template should be added for that rare usage, e.g ``` auto newHeapArray(T)() { import core.memory : GC; alias RT = T[]; return cast(RT*) GC.malloc(RT.sizeof, 0, typeid(T[])); } ```
Comment #3 by b2.temp — 2022-02-02T07:52:57Z
better with calloc of course
Comment #4 by robert.schadek — 2024-12-13T19:20:44Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18082 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB