Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsafe code in C#

Tags:

c#

unsafe

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++?

like image 313
Puppy Avatar asked Jul 03 '10 22:07

Puppy


People also ask

What is unmanaged code and unsafe code?

This is responsible for things like memory management and garbage collection. So unmanaged simply runs outside of the context of the CLR. unsafe is kind of "in between" managed and unmanaged. unsafe still runs under the CLR, but it will let you access memory directly through pointers.

What is unsafe keyword?

The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers. For more information, see Unsafe Code and Pointers. You can use the unsafe modifier in the declaration of a type or a member. The entire textual extent of the type or member is therefore considered an unsafe context.

Why C language is not safe?

C does not have any of these protections: C heap values are created in a type-unsafe way. C casts, unchecked array accesses, and unsafe deallocation can corrupt memory during its lifetime. C deallocation is unsafe, and can lead to dangling pointers.

What is a unsafe class in C#?

Unsafe is a C# programming language keyword to denote a section of code that is not managed by the Common Language Runtime (CLR) of the . NET Framework, or unmanaged code. Unsafe is used in the declaration of a type or member or to specify a block code.


3 Answers

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 different pointer types in C#:

fixed (Byte* dstBytes = &currentImage[0])
{
    var dstBuffer = (Int64*)dstBytes;
    const int blockCount = ImageSizeInBytes / sizeof(Int64);

    for (var j = 0; j < blockCount; j++)
    {
        dstBuffer[j] = srcBuffer[j];
    }
}

Note the type of the array is Byte[], but after I get a Byte* I can cast it to Int64* and work with 8 bytes at a time.

like image 147
codekaizen Avatar answered Oct 06 '22 04:10

codekaizen


Yes, that's all possible. Here's the Unsafe Code Tutorial from MSDN.

To all those saying how using this is a horrible idea: yes, but it's there for a reason. I had to use this (for the first time) just recently, getting webcam data via a third-party API that returned Byte *.

like image 20
egrunin Avatar answered Oct 06 '22 06:10

egrunin


Yes, you can make a pointer point anywhere you like.

However, as your program is running in a virtual address space, you can only access the memory that actually exist in that space, i.e. you can't access any other processes, and you can't access memory that hasn't been allocated.

like image 32
Guffa Avatar answered Oct 06 '22 05:10

Guffa