unsafe

Should you use pointers (unsafe code) in C#?

浪子不回头ぞ 提交于 2019-11-27 18:43:20
Should you use pointers in your C# code? What are the benefits? Is it recommend by The Man (Microsoft)? From "The Man" himself: The use of pointers is rarely required in C#, but there are some situations that require them. As examples, using an unsafe context to allow pointers is warranted by the following cases: Dealing with existing structures on disk Advanced COM or Platform Invoke scenarios that involve structures with pointers in them Performance-critical code The use of unsafe context in other situations is discouraged. Specifically, an unsafe context should not be used to attempt to

C# performance - Using unsafe pointers instead of IntPtr and Marshal

僤鯓⒐⒋嵵緔 提交于 2019-11-27 17:49:07
Question I'm porting a C application into C#. The C app calls lots of functions from a 3rd-party DLL, so I wrote P/Invoke wrappers for these functions in C#. Some of these C functions allocate data which I have to use in the C# app, so I used IntPtr 's, Marshal.PtrToStructure and Marshal.Copy to copy the native data (arrays and structures) into managed variables. Unfortunately, the C# app proved to be much slower than the C version. A quick performance analysis showed that the above mentioned marshaling-based data copying is the bottleneck. I'm considering to speed up the C# code by rewriting

When to use pointers in C#/.NET?

喜欢而已 提交于 2019-11-27 17:16:51
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? When is this needed? Under what circumstances does using pointers becomes inevitable? When the net cost of a managed, safe solution is unacceptable but the net cost of

How can I guarantee that a type that doesn't implement Sync can actually be safely shared between threads?

青春壹個敷衍的年華 提交于 2019-11-27 16:08:33
I have code that creates a RefCell and then wants to pass a reference to that RefCell to a single thread: extern crate crossbeam; use std::cell::RefCell; fn main() { let val = RefCell::new(1); crossbeam::scope(|scope| { scope.spawn(|| *val.borrow()); }); } In the complete code, I'm using a type that has a RefCell embedded in it (a typed_arena::Arena ). I'm using crossbeam to ensure that the thread does not outlive the reference it takes. This produces the error: error: the trait bound `std::cell::RefCell<i32>: std::marker::Sync` is not satisfied [E0277] scope.spawn(|| *val.borrow()); ^~~~~ I

Why do I get the error “Unsafe code may only appear if compiling with /unsafe”?

让人想犯罪 __ 提交于 2019-11-27 09:36:50
问题 Why do I get the following error? Unsafe code may only appear if compiling with /unsafe"? I work in C# and Visual Studio 2008 for programming on Windows CE. 回答1: To use unsafe code blocks, the project has to be compiled with the /unsafe switch on. Open the properties for the project, go to the Build tab and check the Allow unsafe code checkbox. 回答2: Here is a screenshot: ََََََََ 回答3: Probably because you're using unsafe code. Are you doing something with pointers or unmanaged assemblies

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

冷暖自知 提交于 2019-11-27 09:29:35
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(move || { std::ptr::write(target.get_mut(), 10 + i as u32); }); guard }); Is there anything I have

True Unsafe Code Performance

江枫思渺然 提交于 2019-11-27 09:12:46
I understand unsafe code is more appropriate to access things like the Windows API and do unsafe type castings than to write more performant code, but I would like to ask you if you have ever noticed any significant performance improvement in real-world applications by using it when compared to safe c# code. Some Performance Measurements The performance benefits are not as great as you might think. I did some performance measurements of normal managed array access versus unsafe pointers in C#. Results from a build run outside of Visual Studio 2010, .NET 4, using an Any CPU | Release build on

How can I get an array or a slice from a raw pointer?

夙愿已清 提交于 2019-11-27 07:07:22
问题 Can I somehow get an array from std::ptr::read ? I'd like to do something close to: let mut v: Vec<u8> = ... let view = &some_struct as *const _ as *const u8; v.write(&std::ptr::read<[u8, ..30]>(view)); Which is not valid in this form (can't use the array signature). 回答1: If you want to obtain a slice from a raw pointer, use std::slice::from_raw_parts(): let slice = unsafe { std::slice::from_raw_parts(some_pointer, count_of_items) }; If you want to obtain a mutable slice from a raw pointer,

Converting a Vec<u32> to Vec<u8> in-place and with minimal overhead

别来无恙 提交于 2019-11-27 07:06:58
问题 I'm trying to convert a Vec of u32 s to a Vec of u8 s, preferably in-place and without too much overhead. My current solution relies on unsafe code to re-construct the Vec . Is there a better way to do this, and what are the risks associated with my solution? use std::mem; use std::vec::Vec; fn main() { let mut vec32 = vec![1u32, 2]; let vec8; unsafe { let length = vec32.len() * 4; // size of u8 = 4 * size of u32 let capacity = vec32.capacity() * 4; // ^ let mutptr = vec32.as_mut_ptr() as

Temporarily transmute [u8] to [u16]

巧了我就是萌 提交于 2019-11-27 07:02:50
问题 I have a [u8; 16384] and a u16 . How would I "temporarily transmute" the array so I can set the two u8 s at once, the first to the least significant byte and the second to the most significant byte? 回答1: As DK suggests, you probably shouldn't really use unsafe code to reinterpret the memory... but you can if you want to. If you really want to go that route, you should be aware of a couple of gotchas: You could have an alignment problem. If you just take a &mut [u8] from somewhere and convert