Reputation: 11732
In my code I'm calculating an UInt64 value, then casting it like this:
return *(Cell*)packedUInt64;
Cell is a struct. Marshal.SizeOf(new Cell()) prints 8, so it should match an UInt64, but the cast above crashes with AccessViolationException. I could probably try safer methods such as Marshal.StructureToPtr, but I'm still wondering where's the error in my code? Maybe an alignment mismatch problem?
Upvotes: 1
Views: 183
Reputation: 14700
If I understand you correctly, you're calculating a UInt64 value (how?) and then casting that to a POINTER. You're not actually converting it to a Cell struct, but to a memory location in which this Cell resides. The AccessViolationException, in that case, seems perfectly understandable.
Is this what you're actually trying to do, or did I misunderstand your code?
Upvotes: 2