Re: A failing oif C#?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance




"Matt" <matttelles@xxxxxxxxxxx> wrote in message
news:1123615773.907470.209150@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>
> Ken Allen wrote:
>> wackyphill@xxxxxxxxx wrote:
>> > You can infact mark a section of your c# code unsafe and use pointers
>> > just like c++.
>> >
>> How does one mark a section unsafe and have the code before and after
>> that section as safe (managed) code?
>>
>> How does one use pointers at all in C#? Even in unsafe code? If I have a
>> buffer, which is probably defined as a byte array, how do I 'cast' a
>> subset of that array to be treated as a structure with data members? The
>> only way I have seen is to marshal a (cast as IntPtr) specific subset of
>> the array into a structure.
>>
>> -ken
>
> You can do anything in unsafe code, including writing C++ (more or
> less) code:
>
> unsafe
> {
> char s[200];
> int x = 10;
> memcpy(
>

No it's not, you can't call (not directly) unmanaged code like memcpy(...),
calling unmanaged code from C# is only possible through PInvoke and COM
interop.
unsafe doesn't mean "native" it's simply an indication that the block may
contains native C style pointers and can perform pointer arithmetics but
that's it, the code remains managed (compiled to IL) code and the data like
a char array is still GC heap allocated and JIT tracked data.


unsafe {
// allocates a managed array of type char
char[] s = new char[200];
// pin the array object and take the address of the first char in the array
fixed (char* ptr = &s[0])
{
// use ptr f.i as an argument in a call to unmanaged code using
PInvoke interop

}// un-pin the char array!!!
}

Willy.



.



Relevant Pages

  • Re: pass byte-array by reference from c# to managed c++
    ... regarding passing an array to C code. ... private static extern unsafe int dummy; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A failing oif C#?
    ... Willy Denoyette wrote: ... Even in unsafe code? ... >>> subset of that array to be treated as a structure with data members? ... > a char array is still GC heap allocated and JIT tracked data. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A failing oif C#?
    ... How does one mark a section unsafe and have the code before and after that section as safe code? ... How does one use pointers at all in C#? ... If I have a buffer, which is probably defined as a byte array, how do I 'cast' a subset of that array to be treated as a structure with data members? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: passing array to C DLL
    ... In think you might need the Out attribute since the native method is updating the array. ... do really need to mark it as unsafe? ... public static extern unsafe void FFT(floatx, int n); ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Any good refs on dealing with tightly packed data in C#?
    ... this in the past with 'unsafe' code, but I was trying to avoid that. ... A pointer inside an array is just an unsafe way of using an index. ...
    (microsoft.public.dotnet.languages.csharp)