[swift-evolution] [Pitch] Swift needs fixed-size array
Karl Wagner
razielim at gmail.com
Mon Apr 17 14:33:09 CDT 2017
> On 17 Apr 2017, at 21:18, Anders Kierulf <anders at smartgo.com> wrote:
>
>
>> On Apr 17, 2017, at 12:37 PM, Karl Wagner <razielim at gmail.com <mailto:razielim at gmail.com>> wrote:
>>
>>> On 17 Apr 2017, at 19:52, Anders Kierulf via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>
>>> Proposal: UnsafeMutablePointer almost provides what I need. However, it can only allocate memory on the heap, or it can take a given blob of memory and interpret it as something else. What’s missing is a way to allocate typed memory of a certain size on the stack or in a struct. For example, something like this, with support for subscripts, limited to value types:
>>
>> What happens if you use alloca?
>
> Doesn’t look like using alloca is recommended in Swift: https://bugs.swift.org/browse/SR-323 <https://bugs.swift.org/browse/SR-323>
>
> Also, while it might be used as a hack for stack allocations, it wouldn’t allow inclusion into structs.
>
> Anders
Huh. That’s weird. I figured in your case, with an integer literal, there shouldn’t really be any difference between alloca(19*19) and a stack-allocated, fixed-size array like char[19 * 19].
alloca is usually a compiler intrinsic (I suppose that’s why the linker isn’t going to find it). It’s definitely a SIL intrinsic. Maybe those two just aren’t wired up correctly to support user-code calling “alloca”.
Could certainly be supported - we should probably support alloca directly (as we do for malloc), and we could provide a function on UMP which does the same thing (allocateNonEscaping?)
- Karl
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170417/29f481c0/attachment.html>
More information about the swift-evolution
mailing list