Questions tagged [structlayout]

49 questions
25
votes
6 answers

Why does LayoutKind.Sequential work differently if a struct contains a DateTime field?

Why does LayoutKind.Sequential work differently if a struct contains a DateTime field? Consider the following code (a console app which must be compiled with "unsafe" enabled): using System; using System.Runtime.InteropServices; namespace…
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
21
votes
2 answers

Trailing padding in C/C++ in nested structures - is it neccesary?

This is more of a theoretical question. I'm familiar with how padding and trailing padding works. struct myStruct{ uint32_t x; char* p; char c; }; // myStruct layout will compile to // x: 4 Bytes // padding: 4 Bytes // *p: …
Dom324
  • 379
  • 1
  • 9
16
votes
1 answer

Why does the System.DateTime struct have layout kind Auto?

The struct System.DateTime and its cousin System.DateTimeOffset have their structure layout kinds set to "Auto". This can be seen with: typeof(DateTime).IsAutoLayout /* true */ or: typeof(DateTime).StructLayoutAttribute.Value /* Auto */ or…
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
13
votes
1 answer

LayoutKind.Sequential not followed when substruct has LayoutKind.Explicit

When running this code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace StructLayoutTest { class Program { unsafe static void Main() { …
Roland Pihlakas
  • 4,246
  • 2
  • 43
  • 64
11
votes
2 answers

C# StructLayout.Explicit Question

I'm trying to understand why the second example below works with no issues, but the first example gives me the exception below. It seems to me that both examples should give an exception based on the description. Can anyone enlighten me? Unhandled…
Taylor Leese
  • 51,004
  • 28
  • 112
  • 141
7
votes
1 answer

Do C#10’s readonly record structs guarantee the same size and alignment of fields as the explicit implementation?

I do stuff where having contiguous data is required. Now with C# 10, we can do public readonly record struct. I like having the automatic ToString feature that records have, among others, so having that done for me is nice. As such, are the…
Water
  • 3,245
  • 3
  • 28
  • 58
7
votes
1 answer

How come C# can handle this obviously idiotic object promotion during run time?

I like the C# language very much. I'm just playing around, and would never use the code below in production code. Obviously the compiler is fooled by the layout of the struct. But how come, that the string on the Super class can still be written and…
Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76
6
votes
2 answers

Should changing the contents of a string like this cause an exception?

Consider the following code: using System; using System.Runtime.InteropServices; namespace Demo { class Program { static void Main(string[] args) { const string test = "ABCDEF"; // Strings are immutable, right? …
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
6
votes
3 answers

`PROCESSENTRY32W` in C#?

I declared the function Process32FirstW and the structure PROCESSENTRY32W like this: [DllImport("KERNEL32.DLL", CallingConvention = CallingConvention.StdCall, EntryPoint = "Process32FirstW")] private static extern bool Process32FirstW (IntPtr…
Cubi73
  • 1,891
  • 3
  • 31
  • 52
6
votes
7 answers

Is there an alternative for StructLayout "Pack" attribute in Compact Framework?

I would like to do the following: [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct SomeStruct { public byte SomeByte; public int SomeInt; public short SomeShort; public byte SomeByte2; } Is there an…
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
5
votes
3 answers

How can one not make assumptions about C++ struct layouts?

I've just learned from Bug in VC++ 14.0 (2015) compiler? that one shouldn't make assumptions about how a struct's layout will end up in memory. However, I don't understand how it is common practice in a lot of code I've seen. For example, the Vulkan…
4
votes
1 answer

Can I make sure resharper does not change the order of the fields in this class?

I'm using this NetResource class to send files to a network drive and it looks like this: [StructLayout(LayoutKind.Sequential)] public class NetResource { public ResourceScope Scope; public ResourceType ResourceType; public…
Tvde1
  • 1,246
  • 3
  • 21
  • 41
4
votes
2 answers

Shoud I use LayoutKind.Auto for my structs if they don't perform in COM Interop?

By default structs in C# are implemented with [StructLayout( LayoutKind.Sequential )] for reasons basically stating that these type of objects are commonly used for COM Interop and their fields must stay in the order they were defined. Classes have…
Ivan Zlatanov
  • 5,146
  • 3
  • 29
  • 45
4
votes
1 answer

Adding StructLayout attribute to F# type with implicit constructor

I've got: type Package = abstract member Date : int abstract member Save : unit -> unit [] type Instant(date : int, value : int) = let mutable _date = date let…
cnd
  • 32,616
  • 62
  • 183
  • 313
3
votes
1 answer

Marshal.Sizeof() returning unexpected value

I'm debugging code in C# written by a 3rd party. The project is an old C++ project that was rewritten in C# by a contractor, and I have no access to the contractor. I authored the original C++ version. The issue is when the C# code gets the size of…
buzzard51
  • 1,372
  • 2
  • 23
  • 40
1
2 3 4