Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
if we have a class abc

public class abc
{
int a;
int b;
}
if we create object of class
abc obj=new abc();

where does this object and variables will save? on heap or stack?
Posted
Comments
BillWoodruff 17-Sep-14 2:46am    
I'd like to suggest you think of creating instances of an object every time you use the 'new Constructor, and keep strongly in mind the distinction (as Mehdi and Sergey explain below) between the "object itself" and instances of the object. I say this because of my experience in teaching programming, and observing many students having trouble keeping this in mind which leads to difficulties. cheers, Bill

Classes are created on the heap in c#.
Read this : http://stackoverflow.com/questions/15520378/memory-allocation-of-class-objects[^]
 
Share this answer
 
Comments
CPallini 17-Sep-14 2:03am    
5.
Mehdi Gholam 17-Sep-14 2:05am    
Thanks Carlo!
Sergey Alexandrovich Kryukov 17-Sep-14 3:30am    
Not classes, but class instances. Besides, OP needs to understand that a member/variable of a reference type deals with two objects: reference and referenced object, which is essential and clearly distinguishable in code. Please see my answer.
—SA
The instance of abc, the object itself, goes to the heap. As to the reference to this object, it depends on what obj is. If this is the variable, it is on stack. But if, for example, it is an instance member of some class, it also goes to the heap with the instance of the class. If this is a static member, it goes to the area of static memory.

If something is unclear here, you need to understand that if you have some member or variable of reference type, you are dealing with two different objects: the object itself (always on heap) and the reference (managed pointer) pointing to this object. This is important, because you can have two different references to the same object, one reference, zero references, and so on.

—SA
 
Share this answer
 
Comments
Mehdi Gholam 17-Sep-14 9:40am    
My 5!
Sergey Alexandrovich Kryukov 17-Sep-14 9:43am    
Thank you, Mehdi.
—SA
In this case, obj is stored in the heap.
C#
public class abc
{
  int a; 
  int b;
}
abc obj = new abc();

You may consider obj as a handle or a pointer(if you know C++) to the data. The data pointed to by obj is stored in the heap. Treat obj as an integral data. Consider the following code,
C#
abc a = new abc();
abc b = a;

Now, both a and b are handles to the same data. Here this assignment is not so costly, logically, it just copies an integral value. Unlike C++, in managed languages you can't get the address of the object, as the garbage collector shall move the object. But you can use the GetHashCode function. So,
C#
Console.WriteLine(a.GetHashCode() + " is equal to " + b.GetHashCode());
 
Share this answer
 
Comments
vandana sharma10 18-Sep-14 0:00am    
can I check somehow where variable a and b and object is storing ??
[no name] 18-Sep-14 0:05am    
Are you asking whether you can access the address of the object?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900