StructPadding 1.1.0

dotnet add package StructPadding --version 1.1.0
                    
NuGet\Install-Package StructPadding -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="StructPadding" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="StructPadding" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="StructPadding" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add StructPadding --version 1.1.0
                    
#r "nuget: StructPadding, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package StructPadding@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=StructPadding&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=StructPadding&version=1.1.0
                    
Install as a Cake Tool

StructPadding

StructPadding is a high-performance library for clearing (zeroing) padding bytes in unmanaged structures.

It ensures a deterministic memory state, which is critical for binary comparison (memcmp), hash calculation, and security sanitization.

Installation

Package Download
StructPadding NuGet NuGet
dotnet add package StructPadding

Features

  • Maximum Performance: Uses System.Reflection.Emit (IL Generation) to create dynamic code specific to each structure.
  • Zero Allocation: No memory allocations during method calls (after cache warmup).
  • Nested Support: Correctly handles complex structures with nested unmanaged types.
  • Span-friendly: Optimized processing for arrays and Span<T>.
  • Thread-Safe: Thread-safe caching of memory layouts.

Why is this needed?

In .NET (and other languages), structures are aligned in memory to optimize CPU access. This creates "holes" (padding) between fields containing random garbage.

Example structure:

[StructLayout(LayoutKind.Sequential)]
struct Example
{
    public byte A; // 1 byte
    // --- 3 bytes of garbage (padding) ---
    public int B;  // 4 bytes
}

Problems solved by StructPadding:

  1. Hashing: If you calculate a hash (CRC32, MD5, SHA) from the raw memory of a structure, garbage in the padding will result in different hashes for logically identical objects.
  2. Comparison (memcmp): You cannot simply compare two memory blocks to determine if the structures are equal.
  3. Security: Padding may contain residual data from RAM (passwords, keys) which could leak during serialization or memory dumps.

Usage

Zeroing a single structure

using StructPadding;

[StructLayout(LayoutKind.Sequential)]
public struct MyData
{
    public byte Id;
    public long Value; // There will be 7 bytes of padding before this field
}

public void Example()
{
    MyData data = new MyData { Id = 1, Value = 100 };

    // Before zeroing: padding bytes contain garbage
    // After call: padding bytes are guaranteed to be 0
    Zeroer.Zero(ref data);
}

Zeroing an Array or Span

The ZeroArray method is optimized for processing arrays without unnecessary overhead. It generates code that iterates through memory linearly.

public void ProcessBatch(Span<MyData> batch)
{
    // Fast clearing of the entire array/span
    Zeroer.ZeroArray(batch);
    
    // Now the batch can be safely passed to native code, 
    // saved to disk, or hashed.
}

or

public void ProcessBatch(Span<MyData> batch)
{
    batch.ZeroPadding();
}

Performance

The library analyzes the structure only once (upon first access). Based on this analysis, dynamic IL code (DynamicMethod) is generated to perform zero-writing directly at specific offsets.

This means:

  • No reflection on the hot path.
  • No foreach loops over field lists.
  • Performance is comparable to manually written ptr[offset] = 0 code.

Requirements

The type T must have the unmanaged constraint (structs containing only primitives or other unmanaged structs).

License

MIT License. See LICENSE for details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 263 11/21/2025

1.1.0: Fixed the processing of structures with the InlineArray attribute and fixed fields.
           1.0.1: Added an extension method for T[]
           1.0.0: First version