Placement new / custom allocator invocation

ID

c.maintainability.avoid_placement_new

Severity

low

Remediation Complexity

medium

Remediation Risk

medium

Remediation Effort

medium

Resource

Resource Management

Language

C / C++

Description

Reports a placement-new expression - new (location) T(…​) - which is also how a custom allocator is invoked in C++, since the allocator argument is passed through the same placement syntax.

Rationale

Placement new constructs an object in caller-supplied storage without allocating memory itself, which means the caller now owns two separate responsibilities the compiler will not check for them: the storage must be suitably sized and aligned for the type, and the constructed object must later be destroyed with an explicit destructor call rather than delete, or the underlying storage leaks or is reused while still holding a live object. Getting either step wrong is undefined behavior with no compiler diagnostic.

The following code illustrates the pattern detected by this rule:

void f1(void *buf) {
  // FLAGGED: Placement new / custom allocator invocation
  MyClass *c = new (buf) MyClass();
}

Remediation

Prefer ordinary new/delete, a container, or a smart pointer, which pair allocation and destruction automatically. Where placement new is genuinely required (e.g. a custom memory pool), wrap the construct/destroy pair in a small RAII type so the pairing cannot be forgotten.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved