Memory allocation of class objects [closed]

百般思念 提交于 2019-11-27 03:33:39

问题


I have been asked this question in interview. Please help me find its answer.

Suppose you have a class Employee. There are 2 variables in it - 1. String Name 2. Int Age

Now, Employee emp = new Employee();

Now the Questions asked are :

  1. Where the object emp stored in memory i.e in stack or heap and how ?
  2. Where the name and age variables stored in memory and how?
  3. What does each word in this statement do i.e what does employee do..then emp..then =.. then new.. then employee.. then ()..then ;
  4. What is the difference between the above statement and Employee emp; ? Tell in terms of memory allocation.?

Please reply with your valuable comments.


回答1:


  1. Where the object emp stored in memory i.e in stack or heap and how ?

The question is poorly worded. emp is not an object; emp is a variable which contains a reference to an object.

So let's reword the question:

1 (a) Where is the object referred to by emp stored in memory?

The object referred to by variable emp is stored in long-term storage, also known as "the heap".

1 (b) emp is a variable and therefore represents a storage location. Where is that storage location in memory?

The question does not give enough information to say. Variable emp could be a static field, instance field, or local variable. (It cannot be a formal parameter because of the semicolon.) If a local, it could also be a closed-over outer variable of a lambda, or a local of an iterator block, or of an async method. All of these would change whether the variable's storage is in short-term or long-term storage. If it is in short-term storage it could be on the stack or it could be a register.

2 Where are the name and age variables stored in memory and how?

Since they are fields of a class, the storage locations associated with these variables are always on the long-term heap.

Since name is of type string, the thing that it refers to -- a string -- is also on the heap. (Or, the variable could be null, in which case it doesn't refer to anything.)

3 What does each word in this statement do i.e what does employee do..then emp..then =.. then new.. then employee.. then ()..then ;

The question is extremely badly worded. First off, those are not "words", those are "tokens". (And () is two tokens.) Second, it is completely unclear what the question means by "do". So let's ask a different question:

3 Describe in detail the operations performed when this declaration executes at runtime.

We cannot say with any precision because there is not enough information in the question. The question says that it is a statement, so it is not a field declaration. Let's assume for simplicity's sake that it is not in an iterator block or async method, and that the local is not an outer variable of any anonymous function.

  • First, short-term storage is allocated for the variable; it is likely to be enregistered; if not, it will be on the stack. It is assigned a null reference.

  • Second, the memory allocator is asked to produce empty memory for an instance of Employee on the heap. It does so and produces a reference to that memory.

  • Third, if this is the first time we've seen Employee and Employee has a static constructor, the static constructor runs.

  • Fourth, after the static ctor completes, Employee's field initializers run.

  • Fifth, Employee's base class constructor runs. This might cause other static constructors to execute.

  • Sixth, Employee's constructor body runs.

  • Seventh, the constructor completes and the reference to the now-initialized object is copied into its storage.

All this of course assumes that nothing along the way threw an exception.

4 What is the difference between the above statement and Employee emp; ? Tell in terms of memory allocation.

The question does not contain enough information to give an accurate answer. If the local variable is never used then the compiler is free to optimize it away. If it does not optimize it away then the storage for emp is allocated off the short-term pool, initialized to null, and never used.




回答2:


You should see: The Stack Is An Implementation Detail, Part One and Part Two By Eric Lippert

1 - Where the object emp stored in memory i.e in stack or heap and how ?

On heap, because its a reference type since Employee is a class.

2 - Where the name and age variables stored in memory and how?

They are also stored on heap. Although age is a value type, but value types are stored where their container reference is stored.

3 - What does each word in this statement do i.e what does employee do..then emp..then =.. then new.. then employee.. then ()..then ;

Create a new instance of Employee class named emp

4- What is the difference between the above statement and Employee emp; ? Tell in terms of memory allocation.?

Employee emp; means just declaration, not instantiation. That means no memory is allocated to the object and it will hold null.




回答3:


Where the object emp stored in memory i.e in stack or heap and how ?

It's allocated on heap, as you said that Employee is a class. Classes are allocated on heap.

Where the name and age variables stored in memory and how?

This depends on how you used them and can vary between different implemenation of frameworks. In general, think of it as if they ends up with the object's memory itself.

What does each word in this statement do i.e what does employee do..then emp..then =.. then new.. then employee.. then ()..then ;

You allocated nececssary memory for the type Employee and assign it to the pointer emp. The pointer emp is pushed on stack or heap, based on how it's used.

What is the difference between the above statement and Employee emp; ? Tell in terms of memory allocation.?

Employee emp; 

is the same as writing Employee emp = default(Employee); and for reference types default is a null, so this is equal to writing Employee emp = null;. So in terms of allocation there should not to be any difference.

For further reading:

C# Heap(ing) Vs Stack(ing) in .NET: Part I



来源:https://stackoverflow.com/questions/15520378/memory-allocation-of-class-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!