<!--
{
  "documentType" : "article",
  "framework" : "Xcode",
  "identifier" : "/documentation/Xcode/use-of-out-of-scope-stack-memory",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Use of out-of-scope stack memory"
}
-->

# Use of out-of-scope stack memory

Detects access to variables outside of their declared scope.

## Overview

Use this check to detect when you access a variable outside of its scope. Attempting to access out-of-scope memory can result in unpredictable behavior. Available in Xcode 9 and later.

### Use of out-of-scope stack memory in C

In the following example, the code conditionally assigns the `pointer` variable to the return value of the `integer_returning_function` function, which it then accesses out of its declaration scope:

```occ
int *pointer = NULL;
if (bool_returning_function()) {
    int value = integer_returning_function();
    pointer = &value;
}
*pointer = 42; // Error: invalid access of stack memory out of declaration scope
```

#### Solution

Ensure you don’t access variables outside of their declared scope, or allocate memory using the `malloc` function.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)