String copying fails when output from assert

David Zhang straivers98 at gmail.com
Tue Nov 21 18:59:14 UTC 2017


On Tuesday, 21 November 2017 at 18:56:03 UTC, Adam D. Ruppe wrote:
> On Tuesday, 21 November 2017 at 18:49:40 UTC, David  Zhang 
> wrote:
>> 	assert(false, chars[0..str.length]);
>> }
>>
>> What am I missing here?
>
> You're escaping a reference to a local variable there. chars[] 
> is a pointer to the stack, which is promptly smashed when the 
> assert error starts working up the call chain, so by the time 
> it actually prints, you have undefined behavior.

So I'd have to allocate the buffer on the heap then...
Is there any way to do this without allocating to the stack?


More information about the Digitalmars-d-learn mailing list