swiftstructmemory-managementstatic-variables

Where static variables stored? Heap or stack?


What happens when we define static variable in struct? Where it stores? Struct stored variables are known to be stored in stack for each object. But where static variables that accessible from all the code stored?

Or it is stored in special

struct City {
   var name: String
   static var maxCityPopulation: Int = 1000 // where is it stored?? 
}

It seems that static variables should be stored with meta info of structs. But I can't find info where it is stored.


Solution

  • According to this source: https://varun04tomar.medium.com/open-nuts-and-bolts-of-memory-management-in-ios-swift-part-1-4927b60fccf8

    Neither.

    Besides Heap and Stack there is also a Data space that where static variables and type metadata lives.