unsafe

LayoutKind.Sequential not followed when substruct has LayoutKind.Explicit

旧巷老猫 提交于 2019-12-18 04:41:02
问题 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() { Console.WriteLine(IntPtr.Size); Console.WriteLine(); Sequential s = new Sequential(); s.A = 2; s.B = 3; s.Bool = true; s.Long = 6; s.C.Int32a = 4; s.C.Int32b = 5; int* ptr = (int*)&s; Console.WriteLine(ptr[0]); Console.WriteLine(ptr[1]); Console.WriteLine(ptr[2]); Console

When to use pointers in C#/.NET?

依然范特西╮ 提交于 2019-12-17 15:26:52
问题 I know C# gives the programmer the ability to access, use pointers in an unsafe context. But When is this needed? At what circumstances, using pointers becomes inevitable? Is it only for performance reasons? Also why does C# expose this functionality through an unsafe context, and remove all of the managed advantages from it? Is it possible to have use pointers without losing any advantages of managed environment, theoretically? 回答1: When is this needed? Under what circumstances does using

Processing vec in parallel: how to do safely, or without using unstable features?

匆匆过客 提交于 2019-12-17 09:57:11
问题 I have a massive vector that I want to be able to load/act on in parallel, e.g. load first hundred thousand indices in one thread, next in another and so on. As this is going to be a very hot part of the code, I have come up with this following proof of concept unsafe code to do this without Arcs and Mutexes: let mut data:Vec<u32> = vec![1u32, 2, 3]; let head = data.as_mut_ptr(); let mut guards = (0..3).map(|i| unsafe { let mut target = std::ptr::Unique::new(head.offset(i)); let guard = spawn

Why doesn't the VS 2015 debug visualizer work on this unsafe struct?

眉间皱痕 提交于 2019-12-14 02:29:55
问题 I have to hash a lot of stuff...and I keep the hash as a kind of content identity. I use these things everywhere. The hashes are 20-byte arrays and I recently changed them to a (seemingly) simple unsafe struct in a c# project that has a ToString() method. However, at runtime, the visualization is always the default value (all zero) - even after the content changes. The only instance data in the struct is a fixed byte array, which gets written-to by a number of methods. Without the ToString()

Cast pointer to generic structure

試著忘記壹切 提交于 2019-12-13 06:37:38
问题 I am traying to cast a pointer to a generic structure (blittable). It all seems fine when I am doing with non-generic structures => then I am able to use Marshal.PtrToStructure(...) but that function does not receive generic structures (Why ?) So I wrote the following: public static object ReadValue<T>(IntPtr ptr) where T : struct { var dm = new DynamicMethod("$", typeof(T), Type.EmptyTypes); ILGenerator il = dm.GetILGenerator(); il.Emit(OpCodes.Ldc_I4, ptr.ToInt32()); il.Emit(OpCodes.Ldobj,

Is there a way to add elements to a container while immutably borrowing earlier elements?

半城伤御伤魂 提交于 2019-12-12 23:15:34
问题 I'm building a GUI and I want to store all used textures in one place, but I have to add new textures while older textures are already immutably borrowed. let (cat, mouse, dog) = (42, 360, 420); // example values let mut container = vec![cat, mouse]; // new container let foo = &container[0]; // now container is immutably borrowed container.push(dog); // error: mutable borrow Is there any kind of existing structure that allows something like this, or can I implement something like this using

C#: Retrieving and using an IntPtr* through reflection

醉酒当歌 提交于 2019-12-12 14:27:54
问题 I'm currently working on some code which reflects over structures that are marshaled back from calls into a native dll. Some of the structs contain IntPtr* fields that point to null-terminated arrays of pointers. These fields require special processing. When reflecting over the structs, I can recognize these fields because they are marked by a custom attribute. The following illustrates what I'm trying to do: public void ProcessStruct(object theStruct) { foreach (FieldInfo fi in theStruct

C# huge size 2-dim arrays

淺唱寂寞╮ 提交于 2019-12-12 10:44:02
问题 I need to declare square matrices in C# WinForms with more than 20000 items in a row. I read about 2GB .Net object size limit in 32bit and also the same case in 64bit OS. So as I understood the single answer - is using unsafe code or separate library built withing C++ compiler. The problem for me is worth because ushort[20000,20000] is smaller then 2GB but actually I cannot allocate even 700MB of memory. My limit is 650MB and I don't understand why - I have 32bit WinXP with 3GB of memory. I

I can seem to get msbuild to build unsafe code blocks

流过昼夜 提交于 2019-12-12 10:35:24
问题 msbuild doesn't seem to allow me build unsafe blocks even though my .csproj specify: <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> ... <AllowUnsafeBlocks>true</AllowUnsafeBlocks> </PropertyGroup> my build command is: msbuild myProject.sln /p:Configuration=Release /p:Platform="Any CPU" /t:Clean,Build 回答1: You showed that the property is set for the Debug configuration. One option is that it's missing for the Release configuration. Also you specified the

C#: Using pointer types as fields?

依然范特西╮ 提交于 2019-12-12 07:46:24
问题 In C#, it's possible to declare a struct (or class) that has a pointer type member, like this: unsafe struct Node { public Node* NextNode; } Is it ever safe (err.. ignore for a moment that ironic little unsafe flag..) to use this construction? I mean for longterm storage on the heap. From what I understand, the GC is free to move things around, and while it updates the references to something that's been moved, does it update pointers too? I'm guessing no, which would make this construction