unsafe

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

杀马特。学长 韩版系。学妹 提交于 2019-11-28 08:47:41
问题 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;

What is the overhead of C# fixed statement on a managed unsafe struct containing fixed arrays?

六月ゝ 毕业季﹏ 提交于 2019-11-28 08:29:06
I've been trying to determine what the true cost of using the fixed statement within C# for managed unsafe structs that contain fixed arrays. Please note I am not referring to unmanaged structs. Specifically, is there any reason to avoid the pattern shown by 'MultipleFixed' class below? Is the cost of simply fixing the data non zero, near zero (== cost similar to setting & clearing a single flag when entering/exiting the fixed scope), or is it significant enough to avoid when possible? Obviously these classes are contrived to help explain the question. This is for a high usage data structure

Cannot take the address of, get the size of, or declare a pointer to a managed type

混江龙づ霸主 提交于 2019-11-28 07:41:16
I've done a fair bit of research, but am stuck now as to why I'm still getting this error. I have a struct with the following attributes: struct Account { //private attributes private double mBalance; private int mAccountNumber; private string mName; private string mDateCreated; } and am trying to do the following: class BankManager { //private attributes private unsafe Account *mAccounts; private unsafe bool *mAccountsAvailable; private int mNumberAccounts; } Even after turning my class Account to a struct, using "unsafe" for the attributes in class BankManager, and telling the compiler it

Why does this code work without the unsafe keyword?

两盒软妹~` 提交于 2019-11-28 04:45:06
In an answer to his own controversial question , Mash has illustrated that you don't need the "unsafe" keyword to read and write directly to the bytes of any .NET object instance. You can declare the following types: [StructLayout(LayoutKind.Explicit)] struct MemoryAccess { [FieldOffset(0)] public object Object; [FieldOffset(0)] public TopBytes Bytes; } class TopBytes { public byte b0; public byte b1; public byte b2; public byte b3; public byte b4; public byte b5; public byte b6; public byte b7; public byte b8; public byte b9; public byte b10; public byte b11; public byte b12; public byte b13;

Generic BitConverter-like method?

青春壹個敷衍的年華 提交于 2019-11-28 04:24:52
问题 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

Java 8 Unsafe: xxxFence() instructions

孤街浪徒 提交于 2019-11-28 03:33:41
In Java 8 three memory barrier instructions were added to Unsafe class ( source ): /** * Ensures lack of reordering of loads before the fence * with loads or stores after the fence. */ void loadFence(); /** * Ensures lack of reordering of stores before the fence * with loads or stores after the fence. */ void storeFence(); /** * Ensures lack of reordering of loads or stores before the fence * with loads or stores after the fence. */ void fullFence(); If we define memory barrier with the following way (which I consider more or less easy to understand): Consider X and Y to be operation types

Retrieving a pixel alpha value for a UIImage (MonoTouch)

巧了我就是萌 提交于 2019-11-28 01:54:19
问题 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? 回答1: 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 *

Converting System.Decimal to System.Guid

好久不见. 提交于 2019-11-27 23:47:59
I have a big dictionary where the key is decimal, but the GetHashCode() of System.Decimal is disasterously bad. To prove my guess, I ran a for loop with 100.000 neigboring decimals and checked the distribution. 100.000 different decimal numbers used only 2 (two!!!) different hashcodes. Decimal is represented as 16 bytes. Just like Guid! But the GetHashCode() distribution of Guid is pretty good. How can I convert a decimal to Guid in C# as cheap as possible? Unsafe code is OK! EDIT: The test was requested, so here is the code: decimal d = 96000000000000000000m; Dictionary<int, int> hashcount =

Deleting C# Unsafe Pointers

我们两清 提交于 2019-11-27 23:12:00
I know using the /unsafe flag in C#, you can use pointers. In C/C++ to delete a pointer you would use free(pointer); and delete pointer; respectively. However, how would you achieve the same effect with the C# pointers? It depends. You use free and delete to free memory allocated with malloc and new . but in general if you do a PInvoke call, then the pointer should be a IntPtr . if you use fixed (or GCHandle ) to obtain a pointer for a managed object, then the memory was allocated from the GC memory For the memory of GC, when you un-pin that memory (exit the fixed block, or release the

C# Unsafe/Fixed Code

最后都变了- 提交于 2019-11-27 19:40:56
Can someone give an example of a good time to actually use "unsafe" and "fixed" in C# code? I've played with it before, but never actually found a good use for it. Consider this code... fixed (byte* pSrc = src, pDst = dst) { //Code that copies the bytes in a loop } compared to simply using... Array.Copy(source, target, source.Length); The second is the code found in the .NET Framework, the first a part of the code copied from the Microsoft website, http://msdn.microsoft.com/en-us/library/28k1s2k6(VS.80).aspx . The built in Array.Copy() is dramatically faster than using Unsafe code. This might