Don't use arrays as stacks

Lutger Blijdestijn lutger.blijdestijn at gmail.com
Sun Sep 25 04:46:40 PDT 2011


Nick Sabalausky wrote:

> "Jonathan M Davis" <jmdavisProg at gmx.com> wrote in message
> news:mailman.160.1316939358.26225.digitalmars-d at puremagic.com...
>> On Sunday, September 25, 2011 03:18:29 Andrew Wiley wrote:
>>>
>>> Isn't this exactly what assumeSafeAppend is for?
> 
> Hmm, I didn't know about that. (Actually, I remember hearing it mentioned
> before, but then totally forgot about it.)
> 
>>
>> and if you're using assumeSafeAppend, then you
>> need to guarantee that nowhere else has a reference to that array
>> (otherwise
>> it's _not_ safe to assume that it's safe to append)
>>
> 
> Would the consequences of failing to do that be any worse (or any
> different at all?) than what I mentioned about:
> 
> "One caveat about this method: If you save a slice of the stack, pop
> elements off the stack, and then push new values back on, the old slice
> you took will likely reflect the new values, not the original ones."
> 
> ...?

It looks like it behaves the same, but the docs mention this: 'Calling this 
function, and then using references to data located after the given array 
results in undefined behavior.' So it is not wise to depend on it.

One thing that wasn't clear to me (but might be obvious): after modifying an 
array, the behavior is reset and you will need to call assumeSafeAppend 
again.




More information about the Digitalmars-d mailing list