unsafe

How close can I get C# to the performance of C++ for small intensive tasks?

I was thinking about the speed difference of C++ to C# being mostly about C# compiling to byte-code that is taken in by the JIT compiler (is that correct?) and all the checks C# does. I notice that it is possible to turn a lot of these functions off, both in the compile options, and possibly through using the unsafe keyword as unsafe co...

What is the underlying reason for not being able to put arrays of pointers in unsafe structs in C#?

If one could put an array of pointers to child structs inside unsafe structs in C# like one could in C, constructing complex data structures without the overhead of having one object per node would be a lot easier and less of a time sink, as well as syntactically cleaner and much more readable. Is there a deep architectural reason why ...

Why is my unsafe code block slower than my safe code?

I am attempting to write some code that will expediently process video frames. I am receiving the frames as a System.Windows.Media.Imaging.WriteableBitmap. For testing purposes, I am just applying a simple threshold filter that will process a BGRA format image and assign each pixel to either be black or white based on the average of th...

Is there any difference between null and 0 when assigning to pointers in unsafe code?

This may seem odd, but in C (size_t)(void*)0 == 0 is not guaranteed by the language spec. Compilers are allowed to use any value they want for null (although they almost always use 0.) In C#, you can assign null or (T*)0 to a pointer in unsafe code. Is there any difference? (long)(void*)0 == 0 (guaranteed or not? put another way: Int...

Help translating Reflector deconstruction into compilable code

So I am Reflector-ing some framework 2.0 code and end up with the following deconstruction fixed (void* voidRef3 = ((void*) &_someMember)) { ... } This won't compile due to 'The right hand side of a fixed statement assignment may not be a cast expression' I understand that Reflector can only approximate and generally I can see a clea...

How do I change the address of a New struct in a loop?!

I'm writing a simple program which is about polynomials using linked lists in C#. The problem I have is that whenever it creates a new struct (node) in the for loop it gives it the same address as the previous node was given. How do I fix that? Here is my struct: struct poly { public int coef; public int pow; public poly* link;} ; An...

Null reference for first memory address between 0 - 65535

I would like to understand a bit more about memory and I was unable to find it from Google, please forgive me if this is silly question. How come the following code, accessing memory address 0(and up to 65535) in C# would throw NullReferenceException byte* pointer = (byte*)0; byte test = *pointer; Thanks a lot in advance! ...

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

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

Calling AuditQuerySystemPolicy() (advapi32.dll) from C# returns "The parameter is incorrect"

The sequence is like follows: Open a policy handle with LsaOpenPolicy() (not shown) Call LsaQueryInformationPolicy() to get the number of categories; For each category: Call AuditLookupCategoryGuidFromCategoryId() to turn the enum value into a GUID; Call AuditEnumerateSubCategories() to get a list of the GUIDs of all subcategories; Ca...

Determine if method is unsafe via reflection

I'm looking for a way to filter out methods which have the unsafe modifier via reflection. It doesn't seem to be a method attribute. Is there a way? EDIT: it seems that this info is not in the metadata, at least I can't see it in the IL. However reflector shows the unsafe modifier in C# view. Any ideas on how it's done? EDIT 2: For my...

.NET: Show the storage location or address of an object?

Is there a way to get the "address" of an object? This is for demonstration purposes, I know this is a bad idea in general and if it works at all then as unsafe code. The project is tuned to allow unsafe code. However my tries were unsuccessful. The code I have so far is not compiling: unsafe static String AddressOf(Object o) { ...

Conversion from void* to object in C#

Hello, In a C# project, I need to pass object parameters by putting references in a structure. i.e. I have a structure passed to a dispatcher struct SOMESTRUCT { public int lpObject; } Where lpObject holds a pointer to a custom object like class SomeClass { private string foo; } And the SOMESTRUCT structure is passed from ...

Unsafe code in C#

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

How to set an int to byte* C#

How can I convert an int to a byte* at a certain index in a byte*? Ideally I would like to have something like: unsafe{ byte* igm=stackalloc byte[8]; igm[4]=4283; } It would set the first part of the bit to igm[4] and the rest into igm[5]. Edit: I realize there may be a lot of possible ways to handle this, i am looking for...

Usefulness of bool* in C#

Can I use bool* in any kind of meaningful way. How would I convert bool* to a byte for instance, or store bool* in a byte My goal is to manage my own memory in a project of mine, the specifics aren't important, just something id like to do. Now I would like to be able to store my own variables, and i happen to need to store a boolean va...

Are there limitations when using unsafe code in .NET?

I see the allow unsafe code option in .NET. Only one of my apps have it set so i can copy an image to a bitmap quickly. Will there be limitations now that i checked off that box? mono seems to run it fine on linux. I dont see any problems so far. ...

CLR on unsafe code and pointer point to array??

how do CLR interact with unsafe code I found various result on Google but i couldn't understand. I am also confused that is Garbage collector work on unsafe code? if yes than how? I cant point pointer to Array,s first element I try this code unsafe{ int[] a = { 4, 5 }; int* b = a; } but I got that error Error :Cannot implicitl...

Calling a C++ function from C# - unbalanced stack

Hello. I have a unmanaged C++ function with the following signature: int function(char* param, int ret) I am trying to call it from C#: unsafe delegate int MyFunc(char* param, int ret); ... int Module = LoadLibrary("fullpathToUnamanagedDll"); IntPtr pProc = GetProcAddress(Module, "functionName"); MyFunc func = (MyFunc)System.Runt...

What is the overhead of the fixed statement when used on an unmanaged struct?

In particular, I'm thinking of a scenario like this: unsafe struct Foo { public int Bar; public Foo* GetMyAddr() { fixed (Foo* addr = &this) return addr; } } Assuming a Foo stored in unmanaged memory, I'm trying to figure out what is involved in evaluating the fi...

Confusion on whether to use fixed with unsafe code and stackalloc

I have a block of code below with a single line commented out. What happens in the CreateArray method is the same thing that the commented out line does. My question is why does it work when the line b->ArrayItems = d is uncommented, but return garbage when commented out. I dont think I have to "fixed" anything, because all of the infor...