unsafe

Pinning pointer arrays in memory

I'm currently working on a ray-tracer in C# as a hobby project. I'm trying to achieve a decent rendering speed by implementing some tricks from a c++ implementation and have run into a spot of trouble. The objects in the scenes which the ray-tracer renders are stored in a KdTree structure and the tree's nodes are, in turn, stored in an ...

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 code... fixed (byte* pSrc = src, pDst = dst) { //Code that copies the bytes in a loop } compared to simply using... Array.Copy(source, target, source.L...

Fixed Statement in C#

We have similar code to the following in one of our projects. Can anyone explain (in simple English) why the fixed statement is needed here? class TestClass { int iMyVariable; static void Main() { TestClass oTestClass = new TestClass(); unsafe { fixed(int* p = &oTestClasst.iMyVari...

Exceptions Thrown (Errors Encountered) After Program Termination

I have an application that seems to throw exceptions only after the program has been closed. And it is very inconsistent. (We all know how fun inconsistent bugs are...) My guess is there is an error during the clean up process. But these memory read/write errors seem to indicate something wrong in my "unsafe" code usage (pointers?). ...

What is the actual function of the C# project setting "Allow unsafe code"

I was wondering if the C# project setting "Allow unsafe code" applies only to unsafe C# code in the project itself, or is it necessary to set this option when linking in a native C++ DLL? What about linking in a managed DLL that itself links to a native DLL? What does this option really do, under the hood? ...

Which is faster - C# unsafe code or raw C++

I'm writing an image processing program to perform real time processing of video frames. It's in C# using the Emgu.CV library (C#) that wraps the OpenCV library dll (unmanaged C++). Now I have to write my own special algorithm and it needs to be as fast as possible. Which will be a faster implementation of the algorithm? Writing an '...

Why is it allowed to return unsafe pointers from within a function?

I've recently seen a couple of open source projects that actually do this; return an unsafe pointer from a function such as: "int* input = this.someIterator.GetUnsafePtr()". From my understanding this has to be completely wrong. Unsafe pointers can only be obtained through 'fixed' statements and certainly those returned from within a f...

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

Should you use pointers in your C# code? What are the benefits? Is it recommend by The Man (Microsoft)? ...

What is the difference between Fixed and Unsafe.

Why are there 2 different ways lock memory in place in .NET? What is the difference between them? ...

Is there a "preprocessor" symbol corresponding to the /unsafe flag?

I'm dealing with a WriteableBitmap in C#. I'm currently using an unsafe code block to directly access the pixels via WriteableBitmap.BackBuffer. I'd rather depend on the /unsafe option, however, so I'm considering using WriteableBitmap.WritePixels instead. Is there some way of conditionally compiling in the "unsafe" version such that ...

C#: Benefit of explicitly stating "unsafe" / compiler option

I understand pointers and the rare need to use them in C# code. My question is: what is the reasoning behind having to explicitly state "unsafe" in a block of code. Additionally, why must a compiler option be changed to allow "unsafe" code? Bottom Line: What in the CLR (or language specs) makes it so we can't just use pointers whenever ...

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 code is fine. Thanks! I am looking for a byte array 4 time longer than the float array (the dimension of the byte array will be 4 times that of the float array, since each float is composed of 4 byt...

C# unsafe value type array to byte array conversions

I use an extension method to convert float arrays into byte arrays: public static unsafe byte[] ToByteArray(this float[] floatArray, int count) { int arrayLength = floatArray.Length > count ? count : floatArray.Length; byte[] byteArray = new byte[4 * arrayLength]; fixed (float* floatPointer = floatArray) { fixed ...

What are the implications of using unsafe code

Aside from the fact that the code itself can access memory directly. What are the other implications of using the "/unsafe" compiler flag and the "fixed" keyword? Are there any knock on effects related to code signing and deployment of my .exe (my app is desktop only)? (This isn't about whether or not I should be doing this, the why is ...

Bitmap editing in unsafe context - how to avoid instability?

I'm trying to use a bitmap in an unsafe context, and am seeing instability in that, e.g., the program runs the first time round but fails the second. Here is the code: private static void RenderBitmap(Graphics g) { const int width = 150, height = 150; using (Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFor...

LockBits Performance Critical Code

I have a method which needs to be as fast as it possibly can, it uses unsafe memory pointers and its my first foray into this type of coding so I know it can probably be faster. /// <summary> /// Copies bitmapdata from one bitmap to another at a specified point on the output bitmapdata /// </summary> /// <param name="sou...

Why does this code work without the unsafe keyword?

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; ...

sql server 2005:is it safe to use @@identity?

i have a procedure in which i am inserting record in employee table.nad getting empid by using @@identity ? when this procedure will be called by more than one user at same time,there can be possibility that it returns identity of some other employee inserted at same time.because there is no lock on identity by system? --code --identit...

C#: Passing address of public variable

When i try to pass the address of a public variable like this: ML.Register("Radius", &lBeacons[i].Radius, 0.0f, 200.0f, 10.0f); I get this error: error CS0212: You can only take the address of an unfixed expression inside of a fixed statement initializer The Register function looks like this: public unsafe void Register(string des...

Can .NET code compiled with the unsafe tag run in Mono?

I have some code that does Bitmap manipulation using the LockBits method and accessing the bitmap data directly using a pointer. This code has to be wrapped in an unsafe block, of course, and I was wondering if this means that the code would not work in Mono. I'm assuming the Bitmap class is available in Mono, but maybe that's another ...