October 23, 2018
On Tuesday, 23 October 2018 at 05:20:10 UTC, test wrote:
>
> scope tmp 	= new ubyte[4];
>
>
> Error: TypeInfo cannot be used with -betterC
>
>
> how to bypass this ?

Just

    scope ubyte[4] tmp;

doesn't do the job ?
It will be on the stack.
October 23, 2018
On Tuesday, 23 October 2018 at 13:02:15 UTC, test wrote:
> On Tuesday, 23 October 2018 at 11:17:03 UTC, Dennis wrote:
>> On Tuesday, 23 October 2018 at 10:18:56 UTC, test wrote:
>>> I can not user static array because the length is a runtime vars.
>> When compiling with dmd and -betterC I got a linker error "undefined reference to '__alloca'", but with LDC it worked fine.
>
>
>
> Thanks,  alloca is what i am look for.
>

Note however that a failure to allocate with alloca most likely will lead to the crash. The failure can happen in restrictive environments, e.g. containers. That something that I experienced myself. Phobos used to have this bug using alloca in Posix std.process code.
October 24, 2018
On Tuesday, 23 October 2018 at 10:18:56 UTC, test wrote:
> On Tuesday, 23 October 2018 at 07:59:53 UTC, Dennis wrote:
>> On Tuesday, 23 October 2018 at 05:36:00 UTC, test wrote:
>>> correct if I am wrong,  I think malloc is on heap and is very slow.
>>
>> Yes, you probably want a static array on the stack:
>>
>> ```
>> scope ubyte[4] tmp = void; // or a default value
>> ```
>>
>> This is analogous to this C (except scope):
>> ```
>> unsigned char tmp[4];
>> ```
>>
>> While 'new' is analogous to 'malloc'.
>
> I can not user static array because the length is a runtime vars.

The nature of the stack is that every additional variable that is created will be added after the previous one (but lower in memory).  Because of this, the compiler needs to know how much space to reserve for your array.

A standard practice in C to get around this problem is to allocate an array on the stack (using a static array in D), but make the array set to the size of the maximum number of elements you may need.  It acts like a buffer.

When you are done loading data into it, you can either send it to a dynamic array if you want to keep it on the HEAP, or you can use a separate variable to keep track of the actual number of elements inside.
1 2
Next ›   Last »