Primitive Data Type In C Sharp Day 20

Lecture 20 : Primitive Data Type In C Sharp

Primitive Data Type :
  • The primitive types are predefined by the language and they are named by reserved keywords. 

  • They represent the basic types of the language.

Stack :
  • A stack is a special area of computer's memory which stores temporary variables created by a function. 

  • In stack, variables are declared, stored and initialized during runtime.

  • It is a temporary storage memory. When the computing task is complete, the memory of the variable will be automatically erased. 

  • The stack section mostly contains methods, local variable, and reference variables.

Heap :
  • The heap is a memory used by programming languages to store global variables. 

  • By default, all global variable are stored in heap memory space. 

  • It supports Dynamic memory allocation.

  • The heap is not managed automatically for you and is not as tightly managed by the CPU. 

  • It is more like a free-floating region of memory.

Difference between Stack & Heap?

  • The Stack is more or less responsible for keeping track of what's executing our code.

  • The Heap is more or less responsible for keeping track of our objects.

  • The Stack is self-maintaining,that it basically takes care of own memory management.

  • The Heap,on the other hand, has to worry about GC which deals with how to keep the Heap clean.

  • Example : 

           public void Method1()

          {  

           // Line 1 
              int i=4;

          // Line 2  
              int y=2;

          // Line 3 
              class1 cls1 = new class1();

          }
  • It’s a three line code, let’s understand line by line how things execute internally.

  • Memory allocation and de-allocation is done using LIFO (Last In First Out) logic. In other words memory is allocated and de-allocated at only one end of the memory, i.e., top of the stack.

  • Line 1: When this line is executed, the compiler allocates a small amount of memory in the stack. The stack is responsible for keeping track of the running memory needed in your application.

  • Line 2: Now the execution moves to the next step. As the name says stack, it stacks this memory allocation on top of the first memory allocation. You can think about stack as a series of compartments or boxes put on top of each other.

  • Line 3: In line 3, we have created an object. When this line is executed it creates a pointer on the stack and the actual object is stored in a different type of memory location called ‘Heap’. ‘Heap’ does not track running memory, it’s just a pile of objects which can be reached at any moment of time. Heap is used for dynamic memory allocation.

  • One more important point to note here is reference pointers are allocated on stack. 

  • The statement, Class1 cls1; does not allocate memory for an instance of Class1, it only allocates a stack variable cls1.

  • The time it hits the new keyword, it allocates on "heap".

  • The big catch – It did not de-allocate the heap memory. 

  • This memory will be later de-allocated by the garbage collector.

  • Primitive data types are not complex, they hold single values like 'int i = 0;' Object data types are complex, they reference other objects or other primitive data types. 

  • In other words, they hold reference to other multiple values and each one of them must be stored in memory. 

  • Object types need dynamic memory while primitive ones needs static type memory.

  • If the requirement is of dynamic memory, it’s allocated on the heap or else it goes on a stack.

What goes on the Stack and Heap?
  • We have two main types of things we'll be putting in the Stack and Heap as our code is executing: Value Types, Reference Types.

1) Value Type :
  • Value types are types which hold both data and memory on the same location. 

  • When we assign the int value to the other int value, it creates a completely different copy. In other words, if you change either of them, the other does not change. 

  • These kinds of data types are called as ‘Value types’.

2) Reference Type :
  • A reference type has a pointer which points to the memory location.

  • When we create an object and when we assign an object to another object, they both point to the same memory location as shown in the below code snippet. 

  • So when we assign obj to obj1, they both point to the same memory location.

  • In other words if we change one of them, the other object is also affected; this is termed as ‘Reference types’.

Which data types are ref types and which are value types?
  • In .NET depending on the data type, the variable is either assigned on the stack or on the heap. 

  • ‘String’ and ‘Objects’ are reference types, and any other .NET primitive data types are assigned on the stack.

  • The figure below explains the same in a more detail manner :



Program-

Comments

Popular posts from this blog

Basic Concept In C Sharp Day 16

VS Code In JavaScript Day 6

ASP.NET MVC Interview Questions Part - II Day 26