c# - Memory allocation on object creation -
how memory allocated in stack in heap when instantiate class/struct student?
i guess id = 4 bytes reference (32 bit machine) , name = 4 bytes reference , facultyadvisor = 4 bytes reference. totally 12 bytes in stack , actual size in heap. heap size may vary depends upon value assign fields(id, name, facultyadvisor) uses object obj(student obj = new student())
the same struct right?
public class student { int id; string name; professor facultyadvisor; } public struct student { int id; string name; professor facultyadvisor; }
assuming 32 bit clr (references 64 bit on 64 bit clr) , taking account heap allocation student
class student{} class professor{}
- stack: 4 bytes
- heap: 4 bytes (int) + 4 bytes (professor reference) + 4 bytes (string reference) + 12 bytes (object header) = 24 bytes
class student{} struct professor{}
- stack: 4 bytes
- heap: 4 bytes (int) + size of processor + 4 bytes (string reference) + 12 bytes (object header) = ?? bytes
struct student{} class professor{}
- stack: 4 bytes (int) + 4 bytes (string reference) + 4 bytes (professor reference) = 12 bytes
- heap: 0 bytes
struct student{} struct professor{}
- stack: 4 bytes (int) + 4 bytes (string reference) + size of professor = ?? bytes
- heap: 0 bytes
Comments
Post a Comment