digitalmars.D.learn - allow on stack with betterC
- test (3/3) Oct 22 2018 scope tmp = new ubyte[4];
- rikki cattermole (3/11) Oct 22 2018 new only exists thanks to druntime hooks.
- test (3/14) Oct 22 2018 so how to do in pure C code ? can we use that method in D ?
- rikki cattermole (2/17) Oct 22 2018 Yup, call malloc or do the allocation on the stack just like in C.
- test (4/23) Oct 22 2018 correct if I am wrong, I think malloc is on heap and is very
- Dennis (10/12) Oct 23 2018 Yes, you probably want a static array on the stack:
- test (2/14) Oct 23 2018 I can not user static array because the length is a runtime vars.
- Dennis (21/23) Oct 23 2018 Then you can either:
- test (5/11) Oct 23 2018 Thanks, alloca is what i am look for.
- FreeSlave (6/14) Oct 23 2018 Note however that a failure to allocate with alloca most likely
- Vijay Nayar (13/32) Oct 24 2018 The nature of the stack is that every additional variable that is
- Tony (3/10) Oct 23 2018 new is also on the heap. I would guess the speed of new and
- Basile B. (5/8) Oct 23 2018 Just
scope tmp = new ubyte[4]; Error: TypeInfo cannot be used with -betterC how to bypass this ?
Oct 22 2018
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.
Oct 22 2018
On Tuesday, 23 October 2018 at 05:23:42 UTC, rikki cattermole wrote:On 23/10/2018 6:20 PM, test wrote:so how to do in pure C code ? can we use that method in D ?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.
Oct 22 2018
On 23/10/2018 6:26 PM, test wrote:On Tuesday, 23 October 2018 at 05:23:42 UTC, rikki cattermole wrote:Yup, call malloc or do the allocation on the stack just like in C.On 23/10/2018 6:20 PM, test wrote:so how to do in pure C code ? can we use that method in D ?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.
Oct 22 2018
On Tuesday, 23 October 2018 at 05:32:14 UTC, rikki cattermole wrote:On 23/10/2018 6:26 PM, test wrote:correct if I am wrong, I think malloc is on heap and is very slow.On Tuesday, 23 October 2018 at 05:23:42 UTC, rikki cattermole wrote:Yup, call malloc or do the allocation on the stack just like in C.On 23/10/2018 6:20 PM, test wrote:so how to do in pure C code ? can we use that method in D ?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.
Oct 22 2018
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'.
Oct 23 2018
On Tuesday, 23 October 2018 at 07:59:53 UTC, Dennis wrote:On Tuesday, 23 October 2018 at 05:36:00 UTC, test wrote:I can not user static array because the length is a runtime vars.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'.
Oct 23 2018
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.
Oct 23 2018
On Tuesday, 23 October 2018 at 11:17:03 UTC, Dennis wrote:On Tuesday, 23 October 2018 at 10:18:56 UTC, test wrote: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.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.
Oct 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: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.On Tuesday, 23 October 2018 at 10:18:56 UTC, test wrote:Thanks, alloca is what i am look for.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.
Oct 23 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: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.On Tuesday, 23 October 2018 at 05:36:00 UTC, test wrote:I can not user static array because the length is a runtime vars.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'.
Oct 24 2018
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:new is also on the heap. I would guess the speed of new and malloc is very similar.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.
Oct 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.
Oct 23 2018