Bug 14421 – Variadic args array force on heap

Status
RESOLVED
Resolution
WORKSFORME
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-04-07T11:14:00Z
Last change time
2016-01-22T15:54:15Z
Assigned to
nobody
Creator
john.loughran.colvin

Comments

Comment #0 by john.loughran.colvin — 2015-04-07T11:14:39Z
char[] foo(char[] args ...) { return args; } This is obviously wrong code, as args can be allocated on the stack. However, it might not be. Currently the only way out is to go `return args.dup` but that can lead to allocating twice.
Comment #1 by yebblies — 2015-04-11T15:56:19Z
What are you asking for here? Note you can overload (T[]) vs (T[]...) which probably gives what you want.
Comment #2 by john.loughran.colvin — 2015-04-11T16:09:22Z
What I want is a way of writing char[] foo(char[] args ...) { return args.dup; } with a guarantee that only one allocation is made.
Comment #3 by yebblies — 2015-04-12T05:50:46Z
Is there a reason you can't overload and forward the variadic version to the non-variadic after calling dup?
Comment #4 by john.loughran.colvin — 2015-04-12T09:49:25Z
(In reply to yebblies from comment #3) > Is there a reason you can't overload and forward the variadic version to the > non-variadic after calling dup? I don't understand how that would help. What I'm concerned about is if making the variadic array argument creates a GC allocation, then the .dup makes another one.
Comment #5 by yebblies — 2015-04-12T10:12:36Z
(In reply to John Colvin from comment #4) > (In reply to yebblies from comment #3) > > Is there a reason you can't overload and forward the variadic version to the > > non-variadic after calling dup? > > I don't understand how that would help. > > What I'm concerned about is if making the variadic array argument creates a > GC allocation, then the .dup makes another one. If you have this: char[] foo(char[] args...) { return foo(args.dup); } char[] foo(char[] args) { return args; } void main() { foo('a', 'b', 'c'); // this will call first overload, and dup foo(['a', 'b', 'c']); // this will call second, no dup } The array will only be copied if the variadic version is called.
Comment #6 by mathias.lang — 2016-01-22T15:54:15Z
@John: Nowadays, trying to just return a variadic argument gives the following error message: `variadic.d(3): Error: escaping reference to variadic parameter a` It doesn't allocate any memory according to both `@nogc` and gdb. So the solution yebblies mention is the only possible approach nowaday.