Cheok Yan Cheng
Cheok Yan Cheng

Reputation: 42642

If String and Array are implemented as struct, does that mean their size are also limited by stack size?

In Swift, String and Array are implemented as struct

And, struct is being created in stack memory.

And, stack memory has very limited size (Is the stack size of iPhone fixed?), compared to heap memory.

Does that also mean?

  1. The length of String is limited by stack memory size?
  2. The total size of all elements hold by Array, is limited by stack memory size?

Upvotes: 0

Views: 99

Answers (1)

Alexander
Alexander

Reputation: 63137

Array, String and every other dynamically sized-collection (ArraySlice, Substring, Set, Dictionary, etc.) are implemented as structs that contain a reference to a heap-allocated buffer.

Almost everything that Array does is functionality that already exists on its internal buffer. But the Array struct is responsible for implementing the copy-on-write functionality that's so pervasively relied-upon in the Swift world.

Upvotes: 4

Related Questions