Hello All,

Where is the memory allocated for Value TYpes which are part of
a class, when you create an instance of the class, it goes on heap, where is the memory allocate members which are of value types in the class?


Class Customer
{
private int _customerID,
private string _SSN;


}

Regards
LinkPraveen.

Recommended Answers

All 4 Replies

All the fields of a class are allocated in the same place, right next to one another. That means variables whose type is some value type are allocated in the same place as variables whose type is some reference type. Of course, the amount of space needed per variable might be different.

(Edit: I'm saying that the int "_customerID" will get allocated right next to the string reference "_SSN". _SSN is a reference that points to a string allocated elsewhere.

All the fields of a class are allocated in the same place, right next to one another. That means variables whose type is some value type are allocated in the same place as variables whose type is some reference type. Of course, the amount of space needed per variable might be different.

(Edit: I'm saying that the int "_customerID" will get allocated right next to the string reference "_SSN". _SSN is a reference that points to a string allocated elsewhere.

I meant to say that "Value Types" goes into class and "Reference" Types goes into HEAP, in this case when we have both types with a class,
will the value types part of class get allocated on stack and reference types on heap, or class gets allocated in HEAP with both value and referene types on HEAP?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.