tagged [unsafe]

How to use unsafe code in safe contex?

How to use unsafe code in safe contex? I need to use `SecureString` for a Microsoft's class and i found the following code on the [internet](http://blogs.msdn.com/b/fpintos/archive/2009/06/12/how-to-p...

C# default value of a pointer type

C# default value of a pointer type I have been searching through the C# language spec and I can't find anything which says whether a pointer type (e.g. `int*`) gets initialized with a default value. I...

17 Dec at 16:20

Why must fixed size buffers (arrays) be declared unsafe?

Why must fixed size buffers (arrays) be declared unsafe? Let's say I want to have a value type of 7 bytes (or 3 or 777). I can define it like that: A simpler way to define it is using a fixed buffer `...

1 Mar at 20:8

Converting System.Decimal to System.Guid

Converting System.Decimal to System.Guid 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.00...

6 Mar at 05:1

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

Cannot take the address of, get the size of, or declare a pointer to a managed type 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 ...

8 Nov at 22:41

C# Unsafe/Fixed Code

C# Unsafe/Fixed Code 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 c...

17 Sep at 17:12

How can I display a pointer address in C#?

How can I display a pointer address in C#? I've not done any pointers since I've been programming in C# - and my C++ days were long ago. I thought I should refresh my knowledge and was just playing ar...

13 Jan at 15:57

Fast array copy in C#

Fast array copy in C# I have a C# class that contains an int[] array (and a couple of other fields, but the array is the main thing). The code often creates copies of this class and profiling shows th...

23 Apr at 15:27

Why does this unsafe code throw a NullReferenceException?

Why does this unsafe code throw a NullReferenceException? I was playing with unsafe code for a problem on [Code Golf,](https://codegolf.stackexchange.com/q/4399/2718) and I found something I can't exp...

20 Jun at 09:12

Why doesn't *(int*)0=0 cause an access violation?

Why doesn't *(int*)0=0 cause an access violation? For educational purposes, I'm writing a set of methods that cause runtime exceptions in C# to understand what all the exceptions are and what causes t...

30 Dec at 15:46

Does unsafe code have any effect on safe code?

Does unsafe code have any effect on safe code? As I understand it, marking an method as unsafe will disable some of the CLR checks on that code, but does this have any effect on the rest of the system...

27 Apr at 17:24

Simple algorithm to crop empty borders from an image by code?

Simple algorithm to crop empty borders from an image by code? Currently I'm seeking for a rather fast and reasonably accurate algorithm in C#/.NET to do these steps in code: 1. Load an image into memo...

17 Aug at 04:29

What is the fastest way to convert a float[] to a byte[]?

What is the fastest way to convert a float[] to a byte[]? I would like to get a `byte[]` from a `float[]` as quickly as possible, without looping through the whole array (via a cast, probably). Unsafe...

7 Mar at 00:14

C# Bitmap image masking using unsafe code

C# Bitmap image masking using unsafe code I'm using the following code to make image masks in C#: ``` for(int x = 0; x

27 Aug at 06:17

Why does this code work without the unsafe keyword?

Why does this code work without the unsafe keyword? In [an answer](https://stackoverflow.com/questions/791498/how-to-steal-private-data-in-net/791506#791506) to his own [controversial question](https:...

23 May at 11:53

Pointer offset causes overflow

Pointer offset causes overflow The following results don't make any sense to me. It looks like a negative offset is cast to unsigned before addition or subtraction are performed. ``` double[] x = new ...

26 Mar at 18:22

Is GC.KeepAlive required here, or can I rely on locals and arguments keeping an object alive?

Is GC.KeepAlive required here, or can I rely on locals and arguments keeping an object alive? I have a bunch of methods that take the WPF's `WriteableBitmap` and read from its `BackBuffer` directly, u...

23 May at 12:27

C#: Using pointer types as fields?

C#: Using pointer types as fields? In C#, it's possible to declare a struct (or class) that has a pointer type member, like this: Is it ever safe (err.. ignore for a moment that ironic little `unsafe`...

9 Nov at 21:52

How to test handling of AccessViolationException

How to test handling of AccessViolationException I need to write a test which verifies that my code can handle an AccessViolationException (or any other WIN32 Corrupted State Exception - CSE), which o...

23 May at 11:54

Unsafe C# trick to improve speed

Unsafe C# trick to improve speed I am not used to code with pointers (e.g. C++), nor with unsafe islands: only "safe" C#. Now I'd like to implement a function in C# for the .Net Micro Framework, where...

14 Mar at 17:32

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

What is the overhead of the C# 'fixed' statement on a managed unsafe struct containing fixed arrays? I've been trying to determine what the true cost of using the statement within C# for managed unsaf...

3 Jun at 22:23

How to get char** using C#?

How to get char** using C#? I need to pass an argument to an unsafe DllImported function in the form of: I'm assuming it's an array of strings. However when I try to do the following, I get 'Cannot co...

23 May at 12:17

Why can't generic types have explicit layout?

Why can't generic types have explicit layout? If one tries to make a generic struct with the `[StructLayout(LayoutKind.Explicit)]` attribute, using the struct generates an exception at runtime: > Syst...

4 Nov at 23:23

LayoutKind.Sequential not followed when substruct has LayoutKind.Explicit

LayoutKind.Sequential not followed when substruct has LayoutKind.Explicit When running this code: ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using System...

Faster (unsafe) BinaryReader in .NET

Faster (unsafe) BinaryReader in .NET I came across a situation where I have a pretty big file that I need to read binary data from. Consequently, I realized that the default BinaryReader implementatio...