In swift, strings are struct types, so the values are going to be stored in stack memory rather than the heap.
Lets say we declare a string:
let a = ""
let b = "o"
This is an empty string, but some memory is allocated. I need to know what is the difference in the memory given to an empty string and a string with let's say count 1.
If we say:-
var str:String?
var str: String = ""
The difference in memory layout by drawing is what I need to see. (stack / heap).
I tried to find this on google but nowhere I can't find the memory layout for this.
Generally speaking, Swift has 2 fundamental parts for memory allocations. Keep in mind that Swift automatically allocates memory in either the heap or the stack. 1. Memory that lives in the heap 2. Example of code that gets stored in the heap 3. When the heap is used 1. Memory that lives in the stack 2. Example of code that gets stored in the stack
Memory allocated in the heap is often referred to as dynamic memory allocation. In contrast with stack memory, it’s the programmer’s job to allocate and deallocate memory in the heap. You can think of heap memory as a chunk of memory available to the programmer. The heap memory size depends on the size of virtual memory available to the program.
The ways in which heaps and stacks are used in Swift to store memory have a lot of similarity with how we use data structures. In the next article, we’re going to see what these data structures do and how both are implemented in Swift!
Memory in a C/C++/Java program can either be allocated on a stack or a heap. Prerequisite: Memory layout of C program. Stack Allocation: The allocation happens on contiguous blocks of memory. We call it a stack memory allocation because the allocation happens in the function call stack.
String
is a very complicated beast. It has many different underlying representations, including:
String literals, which are stored as constants in the binary
Small strings, which can store up to 15 UTF-8 code units inline on the stack (on x86 at least)
"Native" strings (i.e strings that haven't been bridged to Objective-C), which are stored in a tail-allocated heap buffer
"Foreign" strings from Obj-C which can be represented by a tagged pointer or heap allocated NSString
object
"Shared" strings which can represent a Swift string literal as a heap allocated Obj-C object
If you want to dive into the nitty gritty details, you can start at StringObject.swift, which has a bunch of comments explaining some of the layout details.
To answer your question about the examples you give:
let a = ""
An empty string has a canonical small string representation which is stored inline on the stack.
let b = "o"
A string literal is stored as a constant in the binary. Therefore no heap allocation is required – a pointer to the string's address in the binary is all that's required.
But even if this wasn't a constant string, for example it was created by:
let o = "O".lowercased()
Then it can still be represented as a small string, which is stored inline on the stack.
var str: String?
String
has an extra inhabitant (at least on x86), meaning that String?
can share the same layout as String
. The value nil
is represented by one of the extra inhabitant bit patterns.
But even if String
didn't have an extra inhabitant, no additional heap allocation would be required to represent String?
over String
– it would only require an additional bit of inline storage.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With