---
title: Use of out-of-scope stack memory
framework: xcode
role: article
role_heading: Article
path: xcode/use-of-out-of-scope-stack-memory
---

# Use of out-of-scope stack memory

Detects access to variables outside of their declared scope.

## Overview

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: 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.

## See Also

### Address Sanitizer

- [Use of deallocated memory](xcode/use-of-deallocated-memory.md)
- [Deallocation of deallocated memory](xcode/deallocation-of-deallocated-memory.md)
- [Deallocation of nonallocated memory](xcode/deallocation-of-nonallocated-memory.md)
- [Use of stack memory after function return](xcode/use-of-stack-memory-after-function-return.md)
- [Overflow and underflow of buffers](xcode/overflow-and-underflow-of-buffers.md)
- [Overflow of C++ containers](xcode/overflow-of-c-containers.md)
