iOS Swift Development: Memory Safety

iOS Swift Development: Memory Safety

memorize.aimemorize.ai (lvl 286)
Section 1

Preview this deck

Conflicting Access to In-Out Parameters

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (5)

Section 1

(5 cards)

Conflicting Access to In-Out Parameters

Front

a function has long-term write access to all of its in-out parameters. he write access for an in-out parameter starts after all of the non-in-out parameters have been evaluated and lasts for the entire duration of that function call. If there are multiple in-out parameters, the write accesses start in the same order as the parameters appear. One consequence of this long-term write access is that you can't access the original variable that was passed as in-out, even if scoping rules and access control would otherwise permit it—any access to the original creates a conflict. For example: var stepSize = 1 func increment(_ number: inout Int) { number += stepSize } increment(&stepSize) // Error: conflicting accesses to stepSize In the code above, stepSize is a global variable, and it is normally accessible from within increment(_:). However, the read access to stepSize overlaps with the write access to number. As shown in the figure below, both number and stepSize refer to the same location in memory. The read and write accesses refer to the same memory and they overlap, producing a conflict.

Back

Understanding Conflicting Access to Memory

Front

access to memory happens when you set the value of a variable or pass an argument to a function. Both read/write access: var one = 1 print("We're number \(one)!")

Back

Conflicting Access to self in Methods

Front

Back

Characteristics of Memory Access

Front

There are three characteristics of memory access to consider in the context of conflicting access: 1. whether the access is a read of a write 2. the duration of the access 3. and the location in memory being accessed Specifically if you have two accesses that meet all of the following conditions: -At least one is a write access -They access the same location in memory -Their durations overlap The duration of a memory access is either instantaneous or long-term An access is instantaneous if its not possible for other code to run after that access starts but before it ends. Most memory access is instantaneous. All read and write accesses in the code listing below are instantaneous

Back

When can memory conflicts happen?

Front

Conflicting access to memory can occur when different parts of your code are trying to access the same location in memory at the same time. This can produce unpredictable or inconsistent behavior.

Back