www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - allow on stack with betterC

reply test <test gmail.com> writes:
scope tmp 	= new ubyte[4];


Error: TypeInfo cannot be used with -betterC


how to bypass this ?
Oct 22 2018
next sibling parent reply rikki cattermole <rikki cattermole.co.nz> writes:
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
parent reply test <test gmail.com> writes:
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 ?
Oct 22 2018
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
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.
Oct 22 2018
parent reply test <test gmail.com> writes:
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.
Oct 22 2018
next sibling parent reply Dennis <dkorpel gmail.com> writes:
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
parent reply test <test gmail.com> writes:
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.
Oct 23 2018
next sibling parent reply Dennis <dkorpel gmail.com> writes:
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
parent reply test <test gmail.com> writes:
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.
Oct 23 2018
parent FreeSlave <freeslave93 gmail.com> writes:
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.
Oct 23 2018
prev sibling parent Vijay Nayar <madric gmail.com> writes:
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.
Oct 24 2018
prev sibling parent Tony <tonytdominguez aol.com> writes:
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.
Oct 23 2018
prev sibling parent Basile B. <b2.temp gmx.com> writes:
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