C
C#2y ago
stuw

✅ Marshal.SizeOf is not what I'm thinking

[StructLayout(LayoutKind.Explicit)]
internal unsafe struct ChunkData
{
public const int CELL_SIZE = 33;
public const int CELL_LENGTH = CELL_SIZE * CELL_SIZE * CELL_SIZE;
public const int CELL_BIT_LENGTH = (CELL_SIZE * CELL_SIZE * CELL_SIZE) / 8 + 1;

public const int SIZE = CELL_LENGTH + CELL_BIT_LENGTH;

[FieldOffset(0)]
public fixed byte volumeData[CELL_LENGTH];

[FieldOffset(CELL_LENGTH)]
public fixed byte maxCellCache[CELL_BIT_LENGTH];
}

[StructLayout(LayoutKind.Explicit)]
internal unsafe struct ChunkDataBackupWrapper
{
public const int SIZE = ChunkData.SIZE + 1;

[FieldOffset(0)]
public bool hasBeenBacked;

[FieldOffset(1)]
public ChunkData chunkData;
}
[StructLayout(LayoutKind.Explicit)]
internal unsafe struct ChunkData
{
public const int CELL_SIZE = 33;
public const int CELL_LENGTH = CELL_SIZE * CELL_SIZE * CELL_SIZE;
public const int CELL_BIT_LENGTH = (CELL_SIZE * CELL_SIZE * CELL_SIZE) / 8 + 1;

public const int SIZE = CELL_LENGTH + CELL_BIT_LENGTH;

[FieldOffset(0)]
public fixed byte volumeData[CELL_LENGTH];

[FieldOffset(CELL_LENGTH)]
public fixed byte maxCellCache[CELL_BIT_LENGTH];
}

[StructLayout(LayoutKind.Explicit)]
internal unsafe struct ChunkDataBackupWrapper
{
public const int SIZE = ChunkData.SIZE + 1;

[FieldOffset(0)]
public bool hasBeenBacked;

[FieldOffset(1)]
public ChunkData chunkData;
}
ChunkDataBackupWrapper.SIZE is one byte smaller than Marshal.SizeOf() ChunkData: 40430 Marshal.SizeOf, 40430 SIZE ChunkDataBackupWrapper: 40432 Marshal.SizeOf, 40432 SIZE I'm trying to read https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.structlayoutattribute.pack?redirectedfrom=MSDN&view=net-7.0 but there's a lot of stuff that I can't wrap my head around. "The alignment of the type is the size of its largest element (1, 2, 4, 8, etc., bytes) or the specified packing size, whichever is smaller." "Byte fields align on 1-byte boundaries, Int16 fields align on 2-byte boundaries, and Int32 fields align on 4-byte boundaries." 1. I'm using bytes/bool everywhere, why didn't the second struct align to 1s? 2. Also is "volumeData" considered as an element of size "1" or size "35937"? 3. What's the rules for the size of the struct? I'm not sure I understand
StructLayoutAttribute.Pack Field (System.Runtime.InteropServices)
Controls the alignment of data fields of a class or structure in memory.
5 Replies
ero
ero2y ago
ero
ero2y ago
moral of the story: don't use marshal.sizeof oh actually i think this is because i removed structlayout.explicit moral of the story: don't use structlayout explicit
reflectronic
reflectronic2y ago
I'm using bytes/bool everywhere, why didn't the second struct align to 1s?
this is your issue. a bool is 4 bytes when marshaled
stuw
stuwOP2y ago
I'm using a weird engine, Quantum Photon and if you don't have [StructLayout.Explicit] it literally doesn't compile lol I'll keep that in mind, thanks.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server