All rules
CA2014Reliability Enabled by default: As warning

Do not use stackalloc in loops

Do not use stackalloc in loops

Microsoft docs

Description

The C# stackalloc expression allocates memory from the current stack frame, and that memory may not be released until the current method call returns. If stackalloc is used in a loop, it can lead to stack overflows due to exhausting the stack memory.

Cause

Using the C# stackalloc expression inside of a loop.

How to fix violations

Move the stackalloc expression outside of all loops in the method.

Example

#pragma warning disable CA2014
// The code that's violating the rule is on this line.
#pragma warning restore CA2014

When to suppress

It may be safe to suppress the warning when the containing loop or loops are invoked only a finite number of times, such that the overall amount of memory allocated across all stackalloc operations is known to be relatively small.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0