What is managed code?
A code which is written to aimed to get the services of the managed runtime environment execution like CLR(Common Language Runtime) in .NET Framework is known as Managed Code. It always implemented by the managed runtime environment instead of directly executed by the operating system. The managed runtime environment provides different types of services like garbage collection, type checking, exception handling, bounds checking, etc. to code automatically without the interference of the programmer. It also provides memory allocation, type safety, etc to the code. The application is written in the languages like Java, C#, VB.Net, etc. are always aimed at runtime environment services to manage the execution and the code written in these types of languages are known as managed code.
In the case of .NET Framework, the compiler always compiles the manages code in the intermediate language(MSIL) and then create an executable. When the programmer runs the executable, then the Just In Time Compiler of CLR compiles the intermediate language in the native code which is specific to the underlying architecture. Here this process is taking place under a managed runtime execution environment so this environment is responsible for the working of the code. The execution of managed code is as shown in the below image, the source code is written in any language of .NET Framework.
The managed code also provides platform independence because when the managed code compiled into the intermediate language, then the JIT compiler compiles this intermediate language in the architecture specific instruction.
What are the advantages of using Managed Code?
- It improves the security of the application like when you use runtime environment, it automatically checks the memory buffers to guard against buffer overflow.
- It implement the garbage collection automatically.
- It also provides runtime type checking/dynamic type checking.
- It also provides reference checking which means it checks whether the reference point to the valid object or not and also check they are not duplicate.
What are the disadvantages of Managed Code?
The main disadvantage of managed language is that you are not allowed to allocate memory directly, or you cannot get the low-level access of the CPU architecture.
What is Unmanaged code?
A code which is directly executed by the operating system is known as Unmanaged code. It always aimed for the processor architecture and depends upon computer architecture. When this code is compiled it always tends to get a specific architecture and always run on that platform, in other words, whenever you want to execute the same code for the different architecture you have to recompile that code again according to that architecture. It always compiles to the native code that is specific to the architecture.
In unmanaged code, the memory allocation, type safety, security, etc are managed by the developer. Due to this, there are several problems related to memory occur like buffer overflow, memory leak, pointer override, etc. The executable files of unmanaged code are generally in binary images, x86 code which is directly loaded into memory. The application written in VB 6.0, C, C++, etc are always in unmanaged code. The execution of unmanaged code is as shown in the below image:
What are the advantages of using Unmanaged Code?
- It provides the low-level access to the programmer.
- It also provides direct access to the hardware.
- It allows the programmer to bypass some parameters and restriction that are used by the managed code framework.
What are the disadvantages of Unmanaged Code?
- It does not provide security to the application.
- Due to the access to memory allocation the issues related to memory occur like memory buffer overflow, etc.
- Error and exceptions are also handled by the programmer.
- It does not focus on garbage collection.