views:

91

answers:

3

I hear/read about it sometimes when talking about .NET, for example "managed code" and "unmanaged code" but I have no idea what they are and what are their differences. What are their difference, by definition? What are the consequences of using either of them? Does this distinction exist in .NET/Windows only?

+2  A: 

This is more general than .NET and Windows. Managed is an environment where you have automatic memory management, garbage collection, type safety, ... unmanaged is everything else. So for example .NET is a managed environment and C/C++ is unmanaged.

Darin Dimitrov
+1  A: 

Managed code is a differentiation coined by Microsoft to identify computer program code that requires and will only execute under the "management" of a Common Language Runtime virtual machine (resulting in Bytecode).

http://en.wikipedia.org/wiki/Managed_code

http://www.developer.com/net/cplus/article.php/2197621/Managed-Unmanaged-Native-What-Kind-of-Code-Is-This.htm

RobertPitt
Yes. To add to this answer, managed code is the code you write normally in .NET (in C#, for instance), using the .NET functions. Native Windows applications using the native Windows API (written in any language, C perhaps), on the other hand, are "unmanaged".
Andreas Rejbrand
why was this dowvoted ?
RobertPitt
+4  A: 

Managed Code

Managed code is what Visual Basic .NET and C# compilers create. It runs on the CLR (Common Language Runtime), which, among other things, offers services like garbage collection, run-time type checking, and reference checking. So, think of it as, "My code is managed by the CLR."

Visual Basic and C# can only produce managed code, so, if you're writing an application in one of those languages you are writing an application managed by the CLR. If you are writing an application in Visual C++ .NET you can produce managed code if you like, but it's optional.

Unmanaged Code

Unmanaged code compiles straight to machine code. So, by that definition all code compiled by traditional C/C++ compilers is 'unmanaged code'. Also, since it compiles to machine code and not an intermediate language it is non-portable.

No free memory management or anything else the CLR provides.

Since you cannot create unmanaged code with Visual Basic or C#, in Visual Studio all unmanaged code is written in C/C++.

Mixing the two

Since Visual C++ can be compiled to either managed or unmanaged code it is possible to mix the two in the same application. This blurs the line between the two and complicates the definition, but it's worth mentioning just so you know that you can still have memory leaks if, for example, you're using a third party library with some badly written unmanaged code.

Here's an example I found by googling:

#using <mscorlib.dll>
using namespace System;

#include "stdio.h"

void ManagedFunction()
{
    printf("Hello, I'm managed in this section\n");
}

#pragma unmanaged
UnmanagedFunction()
{
    printf("Hello, I am unmanaged through the wonder of IJW!\n");
    ManagedFunction();
}

#pragma managed
int main()
{
    UnmanagedFunction();
    return 0;
}
kurige
"Since you cannot create unmanaged code with Visual Basic or C#, all unmanaged code is written in C/C++."? You know, there are other languages than C, C++, C#, and VB. I use Delphi to write unmanaged code. Also, one rather visible difference between managed (.NET) and unmanaged (Win32) code, is that the former can use all the .NET functions, whereas the latter use the native Windows API.
Andreas Rejbrand
There is a syntax error in your first paragraph.
Andreas Rejbrand
The terms 'managed' and 'unmanaged' were invented to distinguish machine code from IR. So, they only really have meaning in the context of **.NET**. The linux kernel also compiles to unmanaged code, but that's not really relevant to the discussion, is it?
kurige
*"There is a syntax error in your first paragraph."* It would be more constructive if you would also point out what the error is.
kurige
"on the CLR **(**Common " This is an unmatched parenthesis.
Andreas Rejbrand
I write native (unmanaged) Windows applications in Delphi.
Andreas Rejbrand
This is getting off-topic, but my point is that you don't have the *option* of writing **managed** code in Delphi, so stating that you write **unmanaged** code in Delphi is highly redundant. In any case, I've added "in visual studio" to the offending sentence. Also, thanks for the heads up on the typo. Somehow I missed it even with a second read-through.
kurige
kurige: OK, now I see your point.
Andreas Rejbrand