Hi,
will MSIL code be generated for the block of unsafe code in a program or it would be machine code
will the memory for the data members in the unsafe code be allocated in the Managed momory or somewhere else
Regards,
Benin.
Hi,
will MSIL code be generated for the block of unsafe code in a program or it would be machine code
will the memory for the data members in the unsafe code be allocated in the Managed momory or somewhere else
Regards,
Benin.
Unsafe code compilation
Pockey
1.) Safe code is code that can be verified at compile-time to be "safe". In other words, the compiler has verified that unmanaged API calls aren't being used, unmanaged pointers aren't being referenced, etc. The IL code generated for unsafe calls is basically just a wrapper for platform-specific API calls -- it's "managed" code in the same sense that calling an assembly-language routine from a C++ function is C++ code.
2.) Unmanaged memory is all the memory on your system that isn't being managed by the CLR system. It is outside the process area of the CLR, which is why, for example, pointers to the managed heap need to be pinned if you want to refer to them from unmanaged code. The CLR system doesn't know -- and doesn't want to know -- anything about unmanaged space. CLR takes responsibility for memory management (allocation, GC, and so forth), but there's no analogous system for unmanaged code. (Well, at least, there's nothing native. I'd guess there are 3rd party libraries which attempt to address this.)
aidimorini
1. Is it that you can't invoke the windows API(unmanaged) calls in the safe code block But it is, isn't
2. will I be able to find the block of unsafe code from an assmebly(complied code) using ildasm i.e, whether the MSIL aware of safe and unsafe code
lemax
1.Yes, MSIL is generated for unsafe code blocks. This works because MSIL has special instructions that allow pointer manipulation.
2.Unsafe code block or not, all the memory allocated using the new operator will be in the managed heap (or stack if we are talking about local variables of some value type). The only way to allocate unmanaged memory is by pinvoking some Windows API or using Marshal.AllocHGlobal.
fugu996
Thanks. I have some more doubts,
1. If MSIL is generated for the unsafe code then what is the difference between safe and unsafe code
2. What are unmanaged memory Is is not under the control of CLR Is it out side the process area of CLR(the 'mscoree')
George Clingerman
1. You can do anything in an unsafe block including call unmanaged API.
2. unsafe is a C# keyword and so, a compiler feature. This is designed to help programmers track the use of unsafe code (you cannot use unsafe if don't specify Allow Unsafe Code as a compiler options for example). The generated IL does not contain (explicitly) any trace of the use of unsafe code. The address of a variable is taken using ldloca instruction but this is also used when using ref/out. A pointer and an integer are added using the add instruction (used for adding any 2 numeric values also). stdind instruction is used to store a value at an address in memory but this is also used in case of ref/out.
However, the JIT compiler of the runtime is capable of analyzing the code while compiling and it will know that the code is unsafe (or to use the standard term, unverifiable).