Questions tagged [structlayout]

49 questions
3
votes
3 answers

structs using FieldOffset unexpected behaviour

I am trying to understand explicit struct layout and struct overlaying and i am not seeing behaviour i expect. Given the code below: class Program { static void Main(string[] args) { byte[] bytes = new byte[17]; bytes[0] =…
Domc
  • 150
  • 1
  • 8
3
votes
1 answer

Is it legit code to tag an object like this?

I've once written a piece of code to add a name to a Task. The code below seems to do the same, but with less code. But I wonder, is it legit. Is it production code ready. What about garbage collection? What about the instance of the class being…
Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76
3
votes
1 answer

unassigned field on explicit struct layout

I want to produce a C# sqrt benchmark, but some sqrt functions require an union for bitwise computation. My union is defined as it : [StructLayout(LayoutKind.Explicit)] struct U { [FieldOffset(0)] public int i; [FieldOffset(0)] …
Florian J
  • 59
  • 8
3
votes
2 answers

Arrays sharing memory in .NET4.0 - is that possible with reflection or StructLayout?

I have huge transient arrays created rapidly. Some are kept, some are GC-d. This defragments the heap and the app consumes approx. 2.5x more memory than it would truly need resulting OutOfMemoryException. As a solution, I would prefer to have one…
user256890
  • 3,396
  • 5
  • 28
  • 45
3
votes
1 answer

Union in c# with StructLayout

I have multiple structs that all starts with a header struct. Like this public struct BaseProtocol { public Header header; public Footer footer; }; The header is public struct Header { public Byte start; public Byte group; …
Calypoter
  • 155
  • 2
  • 11
2
votes
1 answer

Packed struct size is 40 even though member sizes add to 36

struct K is packed and the sum of it's class member sizes is 36. Each member has a static_assert checking this. However, the size of K itself is 40, not 36. K isn't polymorphic etc. Why is K not size 36? Using Clang 16. I'm not aware of an easy way…
intrigued_66
  • 16,082
  • 51
  • 118
  • 189
2
votes
1 answer

Must FieldOffset be used on every class/struct member if it is used at all?

Consider the case where I need to ensure a class/struct is mapped to memory in a very specific way, probably due to the need to match an external protocol: [StructLayout(LayoutKind.Sequential, Pack=1)] public class SYSTEM_INFO { public ulong…
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
2
votes
2 answers

Size of a pointer field in a struct

i have to define a struct works with unsafe code, so i have to set the FieldOffset values of every fields. But i cannot define size of pointer. Here is the code : [StructLayout(LayoutKind.Explicit)] public struct SomeStructO { public…
Ibrahim Ozdemir
  • 613
  • 1
  • 5
  • 18
2
votes
1 answer

Making a StructLayout over a class working as it works over a struct

I would like to better understand the mapping of structs/classes when it comes to deal with unmanaged code. I have defined the following struct: [StructLayout(LayoutKind.Sequential)] public struct ProfileInfo { public int dwSize; …
John-Philip
  • 3,392
  • 2
  • 23
  • 52
2
votes
1 answer

Struct is incorrectly aligned: System.TypeLoadException

I'm trying to create the following struct in C#, its a complex struct which is based on a C one, I tried most of the marshaling options, but I always get 'System.TypeLoadException'. (Additional information: Could not load type…
n00b
  • 21
  • 2
2
votes
2 answers

Modify struct layout from p/invoke

I'm looking for best practice guidance around changing the struct/class layout of objects returned/passed into a p/invoke function. I've searched for an answer to this but maybe I'm just too tired and I'm not searching effectively. The simplest…
hdrpunk
  • 33
  • 2
  • 5
2
votes
2 answers

How to convert fixed byte/char[100] to managed char[] in C#?

What's the best way to convert a fixed byte or char[100] to a managed char[] in C#? I ended up having to use pointer arithmetic and I'm wondering if there is an easier way -- something like a memcpy or another way? using System; using…
Taylor Leese
  • 51,004
  • 28
  • 112
  • 141
1
vote
1 answer

Does work the StructLayout attribute with generic in C#?

There is a code using the StructLayout attribute with generic as below. [StructLayout(LayoutKind.Sequential, Pack = 1)] public class Packet : IOutlinePacket where H : new() where Body1 : IDeviceBody, new() where Body2 :…
jjw
  • 282
  • 3
  • 20
1
vote
1 answer

Is it possible to have a union struct with fixed buffer and another struct with array or string fields in C#?

I'm trying to allocate memory for hundreds of thousands objects to initialize them later from an array of bytes. My goal is to skip memory allocation on each object. That is why I am using C#…
Victor Ponamarev
  • 179
  • 2
  • 11
1
vote
1 answer

RtFieldInfo.FieldType causes System.TypeLoadException: Could not load type 'SubClass' from assembly 'A...' because the format is invalid

I narrowed down the program to: using System; using System.Runtime.InteropServices; abstract class Abstract { public int a; } [StructLayout(LayoutKind.Sequential)] sealed class TestClass : Abstract { public int x; } sealed class Container { …
Carl Walsh
  • 6,100
  • 2
  • 46
  • 50