7

During an interview I was asked to outline the differences between structs and classes in Swift. Among my points, I made the argument that structs are stored in the stack (space for them is reserved at compile-time) whereas classes are stored in the heap (space is allocated at run-time).

The interviewer then said he would really test how deep my knowledge of Swift is and then asked me if structs are always stored in the stack. After some back and forth, he made this statement:

If a struct is more than 3 words long, then it is allocated in the heap

I have done some research online and I cannot seem to find this anywhere. Can anyone confirm or reject the validity of this statement? Also, feel free to add any relevant/valid information.

trincot
  • 317,000
  • 35
  • 244
  • 286
Swifty
  • 839
  • 2
  • 15
  • 40
  • Does [this](https://stackoverflow.com/questions/27441456/swift-stack-heap-understanding) help? – Sweeper Feb 06 '19 at 08:11
  • 3
    Possible duplicate of [Swift stack/heap understanding](https://stackoverflow.com/questions/27441456/swift-stack-heap-understanding) – Scriptable Feb 06 '19 at 08:20
  • If I remember correctly, an *existential container* (a value of a protocol type) has a three word buffer for its value, and larger values are stored on the heap. – Martin R Feb 06 '19 at 08:20
  • 6
    Have a look at https://developer.apple.com/videos/play/wwdc2016/416/?time=1631 – Martin R Feb 06 '19 at 08:25
  • thank you @MartinR, I appreciate your help. I will probably delete this question since there are so many votes to close it. Nevertheless I very much appreciate it! – Swifty Feb 06 '19 at 22:48

0 Answers0