Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Take the following image for example. Assume that a variable inst1 of type CustomStructType is declared as a field (as a result the variable is stored in the Heap) but is assigned with an instance of value type, new CustomStructType( ), within the method.

Where is the instance stored in memory? Is the instance, stored on the Stack or Heap?

https://miro.medium.com/max/1334/1*sxU-ukDogaVdLigKUNYV-Q.jpeg[^]

What I have tried:

Confusion on the following circumstance an instance created at the local level but assigned to a variable declared as a field.
Posted
Updated 28-May-21 19:50pm
v2
Comments
Richard MacCutchan 29-May-21 3:16am    
"Is the instance, stored on the Stack or Heap?"
Yes.

1 solution

OH, for ... despite your protestations, it's the same person as last time, and again you aren't listening to what we tell you. Go back to here: Memory allocation concerning a value of value type?[^] and read what we told you.

Asking the same thing over and over is just going to annoy people.
 
Share this answer
 
Comments
[no name] 29-May-21 4:03am    
In relevance to my previous post, I did some further research concerning the following statement you made "Only when you explicitly create an instance of a class using the new is anything else created, and for reference types (i.e. classes) everything that is defined as part of the class is created on the heap - nothing goes on the stack." And it finally made sense.

That being said in relevance to this repost and my previous post, when a variable of Value type is declared as a field but an instance is created and assigned to it at local level (body of method), then the variable and value (instance created at the local level) assigned to it are both stored on the Heap (because of the fact that the variable was declared as a field), when an instance of the class type is created... is that what you were implying?
Dave Kreskowiak 29-May-21 9:56am    
I see you still have impulse control issues. You're still jumping to insults before thinking.
Dave Kreskowiak 29-May-21 10:04am    
You cannot see the answer from your own image you linked to?

It's easy. The instance of an object created from a class is stored on the heap AND the pointer to that instance is stored on the stack.

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