new T[size] vs .reserve - alloca

Nick Treleaven ntrel-public at yahoo.co.uk
Tue Feb 5 08:37:39 PST 2013


On 05/02/2013 16:17, Nick Treleaven wrote:
> On 03/02/2013 13:22, bearophile wrote:
>> Era Scarecrow:
>>
>>> On Sunday, 3 February 2013 at 09:11:59 UTC, Namespace wrote:
>>>> Sure, but alloca has the same ugly interface as malloc. :/
>>>
>>>  You mean that you have to specify how many raw bytes you want, then
>>> cast it to what you need? I never thought alloca or malloc were that
>>> ugly.
>>
>> The interface of alloca() is bug-prone. And it's not handy if you want
>> to create a 2D or nD array on the stack :-) In bugzilla there is a
>> preliminary request for better and less bug-prone VLAs for D.
>
> ^ I know you're aware of this, but maybe others might not know the
> default-argument alloca wrapping trick:

I've just realized this doesn't work for variable-length allocation:

T[] stack(T)(size_t N, void* m = alloca(T.sizeof * N))

Error: undefined identifier N, did you mean alias T?

N is not visible in the caller's scope.


More information about the Digitalmars-d-learn mailing list