Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 23, 2018 allow on stack with betterC | ||||
---|---|---|---|---|
| ||||
scope tmp = new ubyte[4]; Error: TypeInfo cannot be used with -betterC how to bypass this ? |
October 23, 2018 Re: allow on stack with betterC | ||||
---|---|---|---|---|
| ||||
Posted in reply to test | On 23/10/2018 6:20 PM, test wrote:
>
> scope tmp = new ubyte[4];
>
>
> Error: TypeInfo cannot be used with -betterC
>
>
> how to bypass this ?
new only exists thanks to druntime hooks.
So no by-pass. You just can't use it.
|
October 23, 2018 Re: allow on stack with betterC | ||||
---|---|---|---|---|
| ||||
Posted in reply to rikki cattermole | On Tuesday, 23 October 2018 at 05:23:42 UTC, rikki cattermole wrote:
> On 23/10/2018 6:20 PM, test wrote:
>>
>> scope tmp = new ubyte[4];
>>
>>
>> Error: TypeInfo cannot be used with -betterC
>>
>>
>> how to bypass this ?
>
> new only exists thanks to druntime hooks.
> So no by-pass. You just can't use it.
so how to do in pure C code ? can we use that method in D ?
|
October 23, 2018 Re: allow on stack with betterC | ||||
---|---|---|---|---|
| ||||
Posted in reply to test | On 23/10/2018 6:26 PM, test wrote:
> On Tuesday, 23 October 2018 at 05:23:42 UTC, rikki cattermole wrote:
>> On 23/10/2018 6:20 PM, test wrote:
>>>
>>> scope tmp = new ubyte[4];
>>>
>>>
>>> Error: TypeInfo cannot be used with -betterC
>>>
>>>
>>> how to bypass this ?
>>
>> new only exists thanks to druntime hooks.
>> So no by-pass. You just can't use it.
>
> so how to do in pure C code ? can we use that method in D ?
Yup, call malloc or do the allocation on the stack just like in C.
|
October 23, 2018 Re: allow on stack with betterC | ||||
---|---|---|---|---|
| ||||
Posted in reply to rikki cattermole | On Tuesday, 23 October 2018 at 05:32:14 UTC, rikki cattermole wrote:
> On 23/10/2018 6:26 PM, test wrote:
>> On Tuesday, 23 October 2018 at 05:23:42 UTC, rikki cattermole wrote:
>>> On 23/10/2018 6:20 PM, test wrote:
>>>>
>>>> scope tmp = new ubyte[4];
>>>>
>>>>
>>>> Error: TypeInfo cannot be used with -betterC
>>>>
>>>>
>>>> how to bypass this ?
>>>
>>> new only exists thanks to druntime hooks.
>>> So no by-pass. You just can't use it.
>>
>> so how to do in pure C code ? can we use that method in D ?
>
> Yup, call malloc or do the allocation on the stack just like in C.
correct if I am wrong, I think malloc is on heap and is very slow.
|
October 23, 2018 Re: allow on stack with betterC | ||||
---|---|---|---|---|
| ||||
Posted in reply to test | 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'.
|
October 23, 2018 Re: allow on stack with betterC | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis | 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.
|
October 23, 2018 Re: allow on stack with betterC | ||||
---|---|---|---|---|
| ||||
Posted in reply to test | 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.
Then you can either:
- use malloc anyway (recommended when the array can be quite large)
- allocate a static array with an upper bound, and only use a slice of it
- use alloca to allocate on the stack. Watch out for stack overflows though.
```
import core.stdc.stdlib: alloca;
import core.stdc.stdio: printf;
extern(C) void main()
{
int n = 5;
ubyte[] tmp = (cast(ubyte*) alloca(ubyte.sizeof * n))[0..n];
tmp[4] = 22;
printf("%d", tmp[4]);
}
```
When compiling with dmd and -betterC I got a linker error "undefined reference to '__alloca'", but with LDC it worked fine.
|
October 23, 2018 Re: allow on stack with betterC | ||||
---|---|---|---|---|
| ||||
Posted in reply to test | On Tuesday, 23 October 2018 at 05:36:00 UTC, test wrote:
> On Tuesday, 23 October 2018 at 05:32:14 UTC, rikki cattermole wrote:
>>
>> Yup, call malloc or do the allocation on the stack just like in C.
>
> correct if I am wrong, I think malloc is on heap and is very slow.
new is also on the heap. I would guess the speed of new and malloc is very similar.
|
October 23, 2018 Re: allow on stack with betterC | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis | 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.
to @Tony
when you use "scope tmp = new ubyte[4]", it is much fast then malloc because it is on stack.
|
Copyright © 1999-2021 by the D Language Foundation