Memory allocation for value types...

G

Guest

We know that value types are stored at stack are and reference types are
stored at heap area...

If class is look like this..

class Sample
{
int count;
double sal;
}

class MainEntry
{
static void Main()
{
Sample Obj=new Sample();
}
}


Here where the value types are stored in Sample class?

Thanks,
Veera.
 
M

Michael Nemtsev

Hello veera,

Citing Jon
"Instance variables for a value type are stored in the same context as the
variable that declares the value type. The memory slot for the instance effectively
contains the slots for each field within the instance. That means that a
struct variable declared within a method will always be on the stack, whereas
a struct variable which is an instance field of a class will be on the heap.
"

Read this http://www.yoda.arachsys.com/csharp/memory.html


vk> We know that value types are stored at stack are and reference types
vk> are stored at heap area...
vk>
vk> If class is look like this..
vk>
vk> class Sample
vk> {
vk> int count;
vk> double sal;
vk> }
vk> class MainEntry
vk> {
vk> static void Main()
vk> {
vk> Sample Obj=new Sample();
vk> }
vk> }
vk> Here where the value types are stored in Sample class?
vk>
vk> Thanks,
vk> Veera.
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
J

Jon Skeet [C# MVP]

veera sekhar kota said:
We know that value types are stored at stack are and reference types are
stored at heap area...

Well, you may think you know that, but it's not the truth - or at least
not the whole truth.
If class is look like this..

class Sample
{
int count;
double sal;
}

class MainEntry
{
static void Main()
{
Sample Obj=new Sample();
}
}


Here where the value types are stored in Sample class?

On the heap, as part of the data for the object. It's unfortunate that
so many people blithely state that value types are stored on the stack.
See http://www.pobox.com/~skeet/csharp/memory.html for more information
on this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top