Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object Class Primitive Types Stack and Heap

Tags:

c#

.net

First of all I apologize if it sounds too dumb. but I always find myself asking the following questions.

Since object class is the ultimate base class for all classes in .Net and class is a reference type, reference types are stored on heap so how did anything even get to the stack. I know primitive types get stored on stack and that's where I get confused.

Take int32 for instance, if its a class and its base class is object, then its a reference type, should be on heap. How does it go to stack.

If its not a class then how can we do this

int i = 1;
object obj = i;  

Since I cannot do something like

int i = 1;
MyClass myObj = i;

Edit

From the answers I got, int is struct rather than class. Still the confusion is how a value type's base class is a reference type (object) and its a value type not a reference type.

Another point that came up is that "value-types that belong to a class do not get stored on the stack", I guess everything is inside a class, even the Main method inside a console Program. For me that means all the variable somehow belong to a class? So How do they go to stack?

like image 991
ZedBee Avatar asked Feb 03 '14 06:02

ZedBee


People also ask

Are objects in heap or stack?

Whenever an object is created, it's always stored in the Heap space and stack memory contains the reference to it. Stack memory only contains local primitive variables and reference variables to objects in heap space.

Are primitive types stored in stack or heap?

Primitive types declared locally will be on the stack while primitive types that are defined as part of an object instance are stored on the heap. Local variables are stored on stack while instance and static variables are stored on the heap.

What is primitive object type?

In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods or properties.


1 Answers

First of all I apologize if it sounds too dumb. but I always find myself asking the following questions.

These are very common questions.

Since object class is the ultimate base class for all classes in .Net and class is a reference type, reference types are stored on heap so how did anything even get to the stack.

You are confusing many things. Let's start with:

  • An instance of a value type is a value.
  • A reference is a value.
  • A reference refers to an instance of a reference type. (Or it is null, in which case it refers to nothing.)

OK, so now we know that there are three kinds of things, and two of them are values.

So now let's state your sentence correctly:

The object class is the ultimate base class for all classes in .NET. All instances of classes are instances of reference types. All instances of reference types are stored on heap. How is anything stored on the stack?

Now that we have the question phrased correctly the answer is clear. References and instances of value type can go on the stack.

Let's move on.

I know primitive types get stored on stack and that's where I get confused.

Banish the word "primitive type" from your vocabulary; it is meaningless. Let's rephrase:

I know that instances of value types get stored on stack

No, you do not know that because in order to count as knowledge a statement must be true. That statement is false. Values get stored in variables. Variables are either long-lived or short-lived. Short-lived variables go on the stack. Long-lived variables go on the heap.

Take int32 for instance, if its a class and its base class is object, then its a reference type, should be on heap.

Int32 is not a class. It's a struct. It is not a reference type.

Still the confusion is how a value type's base class is a reference type (object) and its a value type not a reference type.

Mary is a mother. She is female. Are all her children therefore also female?

How does it go to stack.

An integer goes on the stack when the integer is stored in a short-lived variable.

A reference goes on the stack when the reference is stored in a short-lived variable.

Whether a value is a reference or an instance of value type is irrelevant; what matters is how long does the variable live?

If its not a class then how can we do this

int i = 1;
object obj = i;  

Good question. A special class is generated. Think of this as being:

class BoxedInt 
{
    int value;
    public BoxedInt(int v) { this.value = v; }
    public int Unbox() { return this.value; }
}

So then your program fragment is:

int i = 1;
object obj = new BoxedInt(i);

And then

int j = (int)obj;

is the same as

int j = ((BoxedInt)obj).Unbox();

Another point that came up is that "value-types that belong to a class do not get stored on the stack

A better way to say that is: fields of a reference type are long-lived variables. Therefore they are stored on the heap.

I guess everything is inside a class, even the Main method inside a console Program.

Yes, all code is inside a class or struct.

For me that means all the variable somehow belong to a class?

No, a local variable is not a member of a class.

I live in a yellow house. Are all the objects inside of it also therefore yellow?

So How do they go to stack?

A local variable in an ordinary method is short-lived, so it goes on the stack.

Simply stop thinking about the stack as having something to do with what kind of data is being stored. The stack has nothing whatsoever to do with what kind of data is being stored. Think about variables. The stack is the place for short-lived variables. The heap is the place for long-lived variables. The type of the variable is irrelevant.

like image 95
Eric Lippert Avatar answered Oct 13 '22 10:10

Eric Lippert