Using ManagedBuffer as alloca?

Since classes that don’t escape the scope are allocated on the stack, does this mean we can use ManagedBuffer as a swift version of alloca?

    let buffer:ManagedBuffer<Void, UInt8> = .create(minimumCapacity: count){ _ in }
        buffer.withUnsafeMutablePointerToElements 
    {
        ...
    }

is this valid swift?

1 Like

It is valid Swift. Allocating on the stack is an optimization, though, not guaranteed behavior. Your example does seem to work under -O, though!

1 Like