當我們實例化類/結構時,它將在堆棧以及堆中分配多少內存Student?創建對象時的內存分配
我猜id = 4字節引用(32位機器)和名稱= 4字節引用和facultyAdvisor = 4字節引用。所以總共12個字節的堆棧和實際大小將堆在一起。這堆大小可能會有所不同取決於我們分配給使用對象obj(Student obj = new Student())的字段(id,name,facultyAdvisor)的值。
結構也一樣嗎?
public class Student { int id; string name; Professor facultyAdvisor; }
public struct Student { int id; string name; Professor facultyAdvisor; }
不要忘記'name'的字符串引用... –
感謝喬恩 - 由於某種原因,我錯過了名字 - 編輯 –
class Student {} class Professor {} 堆棧:4字節 堆:4字節(int)+ 4字節(Professor Reference)+ 4字節引用)+ 12字節(對象頭)= 24字節 在這種情況下,4個字節(教授引用)將在堆中?如果是這樣,實際對象生活? 另外,正如你說的堆棧中的4個字節,這4個字節的堆棧引用指向所有的字段(id,name和facultyAdvisor)。可能嗎? – Alavudeen