unsafe

Unsafe code in C#

拟墨画扇 提交于 2019-11-29 17:27:04
问题 What are the limitations of unsafe code, in C#? For example, can I do virtually arbitrary pointer casts and arithmetic as if I were using C or C++? 回答1: Yes. All bets are off when unsafe is in play. This is the idea behind "unsafe" - that the "safety" of verifiable types is removed, and you can cast from a pointer of one type to a pointer of another type without the runtime keeping you from shooting yourself in the foot, if you so desire - much like C or C++. Here's an example of using

.NET C# unsafe/fixed doesn't pin passthrough array element?

女生的网名这么多〃 提交于 2019-11-29 15:04:20
I have some concurrent code which has an intermittent failure and I've reduced the problem down to two cases which seem identical, but where one fails and the other doesn't. I've now spent way too much time trying to create a minimal, complete example that fails, but without success, so I'm just posting the lines that fail in case anyone can see an obvious problem. Object lock = new Object(); struct MyValueType { readonly public int i1, i2; }; class Node { public MyValueType x; public int y; public Node z; }; volatile Node[] m_rg = new Node[300]; unsafe void Foo() { Node[] temp; while (true) {

Generic BitConverter-like method?

蓝咒 提交于 2019-11-29 11:02:05
I've recently encountered a situation where I need to create a generic method to read a datatype out of a byte array. I've created the following class: public class DataStream { public int Offset { get; set; } public byte[] Data { get; set; } public T Read<T>() where T : struct { unsafe { int dataLen = Marshal.SizeOf( typeof( T ) ); IntPtr dataBlock = Marshal.AllocHGlobal( dataLen ); Marshal.Copy( Data, Offset, dataBlock, dataLen ); T type = *( ( T* )dataBlock.ToPointer() ); Marshal.FreeHGlobal( dataBlock ); Offset += dataLen; return type; } } } Now, de-allocation issues aside, this code doesn

Retrieving a pixel alpha value for a UIImage (MonoTouch)

三世轮回 提交于 2019-11-29 08:35:57
This question is a duplicate of 1042830 , but MonoTouch-specific. Is there a way that's safer than allocating an IntPtr, drawing into it using CGBitmapContext and then reading bytes at the appropriate offset? I don't know if it's kosher to answer your own question, but: protected CGBitmapContext CreateARGBBitmapContext(CGImage inImage) { var pixelsWide = inImage.Width; var pixelsHigh = inImage.Height; var bitmapBytesPerRow = pixelsWide * 4; var bitmapByteCount = bitmapBytesPerRow * pixelsHigh; //Note implicit colorSpace.Dispose() using(var colorSpace = CGColorSpace.CreateDeviceRGB()) { /

Why is a fixed size buffers (arrays) must be unsafe?

笑着哭i 提交于 2019-11-29 05:23:22
问题 Let's say I want to have a value type of 7 bytes (or 3 or 777). I can define it like that: public struct Buffer71 { public byte b0; public byte b1; public byte b2; public byte b3; public byte b4; public byte b5; public byte b6; } A simpler way to define it is using a fixed buffer public struct Buffer72 { public unsafe fixed byte bs[7]; } Of course the second definition is simpler. The problem lies with the unsafe keyword that must be provided for fixed buffers. I understand that this is

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

房东的猫 提交于 2019-11-28 15:50:19
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. 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. Manoj Attal Here is a screenshot: ََََََََ Probably because you're using unsafe code. Are you doing something with pointers or unmanaged assemblies somewhere? Search your code for unsafe blocks or statements. These are only valid is compiled with /unsafe .

how to add unsafe keyword in web based asp.net application c#

邮差的信 提交于 2019-11-28 14:05:05
Hi how i can use unsafe keyword in web based application for pointers? In windows application we have setting in properties section of project under build tag we can check allow unsafe code checkbox, but in web based application how to allow unsafe code or any other in replacement of unsafe code (Pointer) asp.net c# Thank you. If this is a web application you can simply go to the properties of the project and allow unsafe code as you would do in any standard class library. If it is a web site you could try putting the following in your web.config: <system.codedom> <compilers> <compiler

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

…衆ロ難τιáo~ 提交于 2019-11-28 13:53:53
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 *mut u8; mem::forget(vec32); // don't run the destructor for vec32 // construct new vec vec8 = Vec::from

Temporarily transmute [u8] to [u16]

自闭症网瘾萝莉.ら 提交于 2019-11-28 12:39:10
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? sellibitze 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 it to a &mut [u16] , it could refer to some memory region that is not properly aligned to be

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

梦想的初衷 提交于 2019-11-28 12:36:39
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). Vladimir Matveev 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, use std::slice::from_raw_parts_mut() : let slice = unsafe { std::slice::from_raw_parts_mut