.NET Type System, Part I - The Stack and the Heap
(Page 4 of 6 )
If you are a C++ programmer, you are already familiar with object allocation with the Stack and the Heap. With C++ we need to decide if our object needs to be allocated on the Stack or the Heap, and of course it's a painful task (in comparison with what we have now with C# and .NET).
An object allocated on the Stack gives us fast access to the value, but the problem is that it is lifetime bounded to the method that has allocated the object on the stack, and the object will be destroyed when the method returns or completes execution. On the other hand, objects allocated on the Heap have a lifetime that goes beyond the method that has allocated the object, and can be shared with other classes -- but it costs a little more to allocate (unlike Stack objects). As you know, objects allocated on the Heap must be explicitly managed, and if you forget to free objects, it will lead to memory leak or program failure.
In .NET the life has been Managed for us, and also optimized (because it's managed by the CLR). Now Types are allocated when they are used. I know, this is a bit confusing. In other words, a value-type declared in a method will be allocated on the Stack and a value-type declared in a reference-type will be allocated, WHEN IT'S USED, on the Managed Heap. Let's look at Value-Types and Reference-Types in detail so you can understand what I mean.
Value-Types
Value-Types represent any simple value you may have in your programs, and semantically it's a numerical (3, 65.67) or even a particular kind of data (12/1/2004). For example, a variable of type int is a value-type, and when you declare it the CLR will allocate a four byte memory location to store the value of this variable. So a variable of type int needs a fixed memory size of four bytes to store its value.
Value-Types don't cause any overhead of the CLR because they are located on the stack (most of the types, as we will see) and destroyed when the method returns. The memory locations that has been used for the local variables will be reclaimed. In .NET, Value-Types include the primitive types, User-Defined Value Types (Structures) and Enumerations. Value Types derive from the ValueType base class, which in turn derives from the System.Object base class. The ValueType class extends the functionality provided by the System.Object base class. This functionality includes comparing two instances of value types, which will be discussed in the last article of the series.
When you assign one value-type to another value-type (like x = y) the approach taken here is copy-by-value, which means that the value of the y will be copied into x. Let's use a simple example to explain this behavior. Create a new Visual Studio.NET Console Project and call it ValueTypes, then delete all the code in the Class1.cs and copy the code below. If you are not using VS.NET then you would copy the code below and paste it into a file with a .cs extension, then compile it.
using System;
namespace ValueTypes
{
class Class1
{
static void Main(string[] args)
{
int EnglishGrade = 40;
int FrenchGrade = 35;
Console.WriteLine("The English Grade = {0}", EnglishGrade);
Console.WriteLine("The French Grade = {0}", FrenchGrade);
//here we assign EnglishGrade to FrenchGrade
FrenchGrade = EnglishGrade;
//then we write to the console the value again
Console.WriteLine("(after assignment)The English Grade = {0}", EnglishGrade);
Console.WriteLine("(after assignment)The French Grade = {0}", FrenchGrade);
//read line to just make the console wait for you
Console.ReadLine();
}
}
}
Run the Application and you will get the following result in the console:
The English Grade = 40
The French Grade = 35
(after assignment)The English Grade = 40
(after assignment)The French Grade = 40
It's pretty clear that the value of EnglishGrade has been copied to the variable FrenchGrade (through the assignment statement) and this illustrates the idea of Copy-By-Value. What you can notice here is that the variable FrenchGrade value was 35 before the assignment statement, and after this assignment it has the same value as the EnglishGrade variable, so the CLR performed bit-by-bit copying to have the value of EnglishGrade copied to the variable FrenchGrade.
All the built-in Types (listed in the previous article's CTS built-in types tables) are Value-Types except System.String and System.Object. We will talk about System.String object in the section Reference-Type. There's nothing special about primitive types to discuss at the moment, so we will move directly to Structures and Enumerations.
Next: Structures: complex Value-Types >>
More .NET Articles
More By Michael Youssef