<!--
{
  "documentType" : "article",
  "framework" : "Xcode",
  "identifier" : "/documentation/Xcode/invalid-object-size",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Invalid object size"
}
-->

# Invalid object size

Detects invalid pointer casts due to differences in the sizes of types.

## Overview

Use this check to detect pointer casts when the size of the source type is less than the size of the destination type. Using the result of such a cast to access out-of-bounds data has undefined behavior. Available in Xcode 9 and later.

### Downcast from type with insufficient space in C++

In the following example, the cast from `Base *` to `Derived *` is suspect because `Base` isn’t large enough to contain an instance of `Derived`:

```occ
struct Base {
    int pad1;
};
struct Derived : Base {
    int pad2;
};
Derived *getDerived() {
    return static_cast<Derived *>(new Base); // Error: invalid downcast
}
```

The optimizer may remove an expression, such as `getDerived()->pad2`, because `getDerived()` returns a pointer to an object that isn’t large enough to contain a `pad2` field.

> Note: This UBSan check may not trigger at low optimization levels.

#### Solution

One way to fix this issue is to avoid the downcast, such as by using instances of the `Derived` object wherever you need them.

---

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)