Home All Groups Group Topic Archive Search About

General Question About Structs and Stack

Author
12 Apr 2005 2:14 PM
gmccallum
General Info:
A struct is stored on the stack and a class on the heap.
A struct is a value type while a class is a reference type.

Question:
What if a struct contains a string property(variable).  The string would
be a reference type being contained in a value type.  Would this filter
up and cause the stack to now be a reference type placed on the heap
or would it still be on the stack with a pointer used internally to point
to a preallocated string object on the heap?

The reason for the question is because almost all examples related to
stacks and discussing the speed benefits never show anything in the
stack other than value types (which a internally structs themselves like int).

I am creating a large binary tree and currently use classes to implement it.
There are no problems with the code, but if I could see a major performance
gain by switching the classes to structs I would do it.

What do you think?

-Greg

Author
12 Apr 2005 2:39 PM
Salvador
Hi,

The struct will always remain on the stack, that's  why you can not assign
null to the structure nevertheless you have a reference variable in it.
If you want to move the structure to the heap to test performance you can
use a weakReference to store the value type.

There are some details on the secions 11.3.1, 4.1 and 4.2 of the c#
specification.

Hope this helps to understand.
Salva

Show quote
"gmccallum" wrote:

> General Info:
> A struct is stored on the stack and a class on the heap.
> A struct is a value type while a class is a reference type.
>
> Question:
> What if a struct contains a string property(variable).  The string would
> be a reference type being contained in a value type.  Would this filter
> up and cause the stack to now be a reference type placed on the heap
> or would it still be on the stack with a pointer used internally to point
> to a preallocated string object on the heap?
>
> The reason for the question is because almost all examples related to
> stacks and discussing the speed benefits never show anything in the
> stack other than value types (which a internally structs themselves like int).
>
> I am creating a large binary tree and currently use classes to implement it.
> There are no problems with the code, but if I could see a major performance
> gain by switching the classes to structs I would do it.
>
> What do you think?
>
> -Greg
Author
12 Apr 2005 2:46 PM
gmccallum
So if the struct will remain on the stack (where I want it), then how is it
still
treated as a value type when it contains a reference type.  Does this
seriously
affect performance.

example
struct {
   int x;      // value type
   string s; // ref type
}

-Greg McCallum (MCSD)

Show quote
"Salvador" wrote:

> Hi,
>
> The struct will always remain on the stack, that's  why you can not assign
> null to the structure nevertheless you have a reference variable in it.
> If you want to move the structure to the heap to test performance you can
> use a weakReference to store the value type.
>
> There are some details on the secions 11.3.1, 4.1 and 4.2 of the c#
> specification.
>
> Hope this helps to understand.
> Salva
>
> "gmccallum" wrote:
>
> > General Info:
> > A struct is stored on the stack and a class on the heap.
> > A struct is a value type while a class is a reference type.
> >
> > Question:
> > What if a struct contains a string property(variable).  The string would
> > be a reference type being contained in a value type.  Would this filter
> > up and cause the stack to now be a reference type placed on the heap
> > or would it still be on the stack with a pointer used internally to point
> > to a preallocated string object on the heap?
> >
> > The reason for the question is because almost all examples related to
> > stacks and discussing the speed benefits never show anything in the
> > stack other than value types (which a internally structs themselves like int).
> >
> > I am creating a large binary tree and currently use classes to implement it.
> > There are no problems with the code, but if I could see a major performance
> > gain by switching the classes to structs I would do it.
> >
> > What do you think?
> >
> > -Greg
Author
12 Apr 2005 3:00 PM
Salvador
Hi,

Maybe this can help to understand,

The memory slot for a variable is stored on either the stack or the heap. It
depends on the context in which it is declared:

Each local variable (ie one declared in a method) is stored on the stack.
That includes reference type variables - the variable itself is on the stack,
but remember that the value of a reference type variable is only a reference
(or null), not the object itself. Method parameters count as local variables
too, but if they are declared with the ref modifier, they don't get their own
slot, but share a slot with the variable used in the calling code. See my
article on parameter passing for more details

Instance variables for a reference type are always on the heap. That's where
the object itself "lives".

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
(given the previous two points) 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.

Every static variable is stored on the heap, regardless of whether it's
declared within a reference type or a value type. There is only one slot in
total no matter how many instances are created.

Best regards
SAlva


Show quote
"gmccallum" wrote:

> So if the struct will remain on the stack (where I want it), then how is it
> still
> treated as a value type when it contains a reference type.  Does this
> seriously
> affect performance.
>
> example
> struct {
>    int x;      // value type
>    string s; // ref type
> }
>
> -Greg McCallum (MCSD)
>
> "Salvador" wrote:
>
> > Hi,
> >
> > The struct will always remain on the stack, that's  why you can not assign
> > null to the structure nevertheless you have a reference variable in it.
> > If you want to move the structure to the heap to test performance you can
> > use a weakReference to store the value type.
> >
> > There are some details on the secions 11.3.1, 4.1 and 4.2 of the c#
> > specification.
> >
> > Hope this helps to understand.
> > Salva
> >
> > "gmccallum" wrote:
> >
> > > General Info:
> > > A struct is stored on the stack and a class on the heap.
> > > A struct is a value type while a class is a reference type.
> > >
> > > Question:
> > > What if a struct contains a string property(variable).  The string would
> > > be a reference type being contained in a value type.  Would this filter
> > > up and cause the stack to now be a reference type placed on the heap
> > > or would it still be on the stack with a pointer used internally to point
> > > to a preallocated string object on the heap?
> > >
> > > The reason for the question is because almost all examples related to
> > > stacks and discussing the speed benefits never show anything in the
> > > stack other than value types (which a internally structs themselves like int).
> > >
> > > I am creating a large binary tree and currently use classes to implement it.
> > > There are no problems with the code, but if I could see a major performance
> > > gain by switching the classes to structs I would do it.
> > >
> > > What do you think?
> > >
> > > -Greg
Author
12 Apr 2005 4:53 PM
Jon Skeet [C# MVP]
Salvador <Salva***@discussions.microsoft.com> wrote:
> Maybe this can help to understand,

<snip>

Hopefully it will have done, but while I don't have any problem with
people quoting my web articles in news posts, it would be nice to at
least post a link to the original at the same time.

http://www.pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Author
12 Apr 2005 2:47 PM
gmccallum
So if the struct will remain on the stack (where I want it), then how is it
still
treated as a value type when it contains a reference type.  Does this
seriously
affect performance.

example
struct {
   int x;      // value type
   string s; // ref type
}

-Greg McCallum (MCSD)


Show quote
"Salvador" wrote:

> Hi,
>
> The struct will always remain on the stack, that's  why you can not assign
> null to the structure nevertheless you have a reference variable in it.
> If you want to move the structure to the heap to test performance you can
> use a weakReference to store the value type.
>
> There are some details on the secions 11.3.1, 4.1 and 4.2 of the c#
> specification.
>
> Hope this helps to understand.
> Salva
>
> "gmccallum" wrote:
>
> > General Info:
> > A struct is stored on the stack and a class on the heap.
> > A struct is a value type while a class is a reference type.
> >
> > Question:
> > What if a struct contains a string property(variable).  The string would
> > be a reference type being contained in a value type.  Would this filter
> > up and cause the stack to now be a reference type placed on the heap
> > or would it still be on the stack with a pointer used internally to point
> > to a preallocated string object on the heap?
> >
> > The reason for the question is because almost all examples related to
> > stacks and discussing the speed benefits never show anything in the
> > stack other than value types (which a internally structs themselves like int).
> >
> > I am creating a large binary tree and currently use classes to implement it.
> > There are no problems with the code, but if I could see a major performance
> > gain by switching the classes to structs I would do it.
> >
> > What do you think?
> >
> > -Greg
Author
12 Apr 2005 2:49 PM
James Curran
>> would it still be on the stack with a pointer used internally to point
> to a preallocated string object on the heap?

    As far as I know, that is the case.  I see no advantage, and lot of
problems if it were handled any other way.   Note, that this is exactly know
a string as a local varaible is handled.


Show quote
"gmccallum" <gmccal***@discussions.microsoft.com> wrote in message
news:A39D617B-922E-4027-A6BC-D645A440DC9F@microsoft.com...
> General Info:
> A struct is stored on the stack and a class on the heap.
> A struct is a value type while a class is a reference type.
>
> Question:
> What if a struct contains a string property(variable).  The string would
> be a reference type being contained in a value type.  Would this filter
> up and cause the stack to now be a reference type placed on the heap
> or would it still be on the stack with a pointer used internally to point
> to a preallocated string object on the heap?
>
> The reason for the question is because almost all examples related to
> stacks and discussing the speed benefits never show anything in the
> stack other than value types (which a internally structs themselves like
int).
>
> I am creating a large binary tree and currently use classes to implement
it.
> There are no problems with the code, but if I could see a major
performance
> gain by switching the classes to structs I would do it.
>
> What do you think?
>
> -Greg
Author
12 Apr 2005 2:59 PM
gmccallum
Thanks James and Salvador.  I am trying the get the latest version of the c#
specs from Microsoft, but it appears to be unavailable at the moment.

If it turns out the stack is handled as stated below for a reference type
being included in it, then this would defintely be more effiecient than using
the class when I don't care if it is sealed and don't plain to use it as a
base class to anything.

- Greg McCallum


Show quote
"James Curran" wrote:

> >> would it still be on the stack with a pointer used internally to point
> > to a preallocated string object on the heap?
>
>     As far as I know, that is the case.  I see no advantage, and lot of
> problems if it were handled any other way.   Note, that this is exactly know
> a string as a local varaible is handled.
>
>
> "gmccallum" <gmccal***@discussions.microsoft.com> wrote in message
> news:A39D617B-922E-4027-A6BC-D645A440DC9F@microsoft.com...
> > General Info:
> > A struct is stored on the stack and a class on the heap.
> > A struct is a value type while a class is a reference type.
> >
> > Question:
> > What if a struct contains a string property(variable).  The string would
> > be a reference type being contained in a value type.  Would this filter
> > up and cause the stack to now be a reference type placed on the heap
> > or would it still be on the stack with a pointer used internally to point
> > to a preallocated string object on the heap?
> >
> > The reason for the question is because almost all examples related to
> > stacks and discussing the speed benefits never show anything in the
> > stack other than value types (which a internally structs themselves like
> int).
> >
> > I am creating a large binary tree and currently use classes to implement
> it.
> > There are no problems with the code, but if I could see a major
> performance
> > gain by switching the classes to structs I would do it.
> >
> > What do you think?
> >
> > -Greg
>
>
>
Author
12 Apr 2005 3:03 PM
Salvador
Hi,

A lot of confusion has been wrought by people explaining the difference
between value types and reference types as "value types go on the stack,
reference types go on the heap, this is simply untrue. Check my previous
message.

Hope this helps to solve a common problem.
Salva


Show quote
"gmccallum" wrote:

> Thanks James and Salvador.  I am trying the get the latest version of the c#
> specs from Microsoft, but it appears to be unavailable at the moment.
>
> If it turns out the stack is handled as stated below for a reference type
> being included in it, then this would defintely be more effiecient than using
> the class when I don't care if it is sealed and don't plain to use it as a
> base class to anything.
>
> - Greg McCallum
>
>
> "James Curran" wrote:
>
> > >> would it still be on the stack with a pointer used internally to point
> > > to a preallocated string object on the heap?
> >
> >     As far as I know, that is the case.  I see no advantage, and lot of
> > problems if it were handled any other way.   Note, that this is exactly know
> > a string as a local varaible is handled.
> >
> >
> > "gmccallum" <gmccal***@discussions.microsoft.com> wrote in message
> > news:A39D617B-922E-4027-A6BC-D645A440DC9F@microsoft.com...
> > > General Info:
> > > A struct is stored on the stack and a class on the heap.
> > > A struct is a value type while a class is a reference type.
> > >
> > > Question:
> > > What if a struct contains a string property(variable).  The string would
> > > be a reference type being contained in a value type.  Would this filter
> > > up and cause the stack to now be a reference type placed on the heap
> > > or would it still be on the stack with a pointer used internally to point
> > > to a preallocated string object on the heap?
> > >
> > > The reason for the question is because almost all examples related to
> > > stacks and discussing the speed benefits never show anything in the
> > > stack other than value types (which a internally structs themselves like
> > int).
> > >
> > > I am creating a large binary tree and currently use classes to implement
> > it.
> > > There are no problems with the code, but if I could see a major
> > performance
> > > gain by switching the classes to structs I would do it.
> > >
> > > What do you think?
> > >
> > > -Greg
> >
> >
> >
Author
12 Apr 2005 3:23 PM
gmccallum
What you state is true, you are describing the concept of stack frames.

However the c# documentation states that classes are stored on the heap
which would mean in the current stack frame would exist a pointer to a memory
block on the heap which would contain the actual data.  A struct however
(when it contains only value types) would be totally stored on the stack with
no pointers to the heap.  Which would be a major speed enhancement.

The problem I see is that the moment you create a reference type within the
struct then one of three things could occur:
1) the entire struct would be relocated to heap with a pointer placed in the
stack frame to hold its location.  (BAD)
2) the struct would remain on the stack with a pointer used to contain the
address of the actual string data which has be placed on the heap. (SO SO)
3) the struct would remain on the stack and all the variables defined within
would remain entirely on the stack as well.  (no far pointers - EXCELLANT).

I am beginning to think that option 2 is what happens, but I was hoping for
option 3.

-Greg McCallum
Author
12 Apr 2005 3:59 PM
Salvador
Hi Greg,

It is interesting, so I run the CLR Profiler

http://msdn.microsoft.com/netframework/downloads/tools/default.aspx

You can see the heap there, you can filter to show when a new object is
loaded on the heap, after creating an instance of the struct and populating
the string nothing appared on the heap. Maybe this means good news for you.
Give it a try and keep me posted if you find anything interesting.

My email is Salvapat***@yahoo.co.uk, is good to have people with good
knowledge around.

cheers
Salva


Show quote
"gmccallum" wrote:

> What you state is true, you are describing the concept of stack frames.
>
> However the c# documentation states that classes are stored on the heap
> which would mean in the current stack frame would exist a pointer to a memory
> block on the heap which would contain the actual data.  A struct however
> (when it contains only value types) would be totally stored on the stack with
> no pointers to the heap.  Which would be a major speed enhancement.
>
> The problem I see is that the moment you create a reference type within the
> struct then one of three things could occur:
> 1) the entire struct would be relocated to heap with a pointer placed in the
> stack frame to hold its location.  (BAD)
> 2) the struct would remain on the stack with a pointer used to contain the
> address of the actual string data which has be placed on the heap. (SO SO)
> 3) the struct would remain on the stack and all the variables defined within
> would remain entirely on the stack as well.  (no far pointers - EXCELLANT).
>
> I am beginning to think that option 2 is what happens, but I was hoping for
> option 3.
>
> -Greg McCallum
>
Author
12 Apr 2005 4:20 PM
gmccallum
I will do so.  Thanks for the interaction.
-Greg


Show quote
"Salvador" wrote:

> Hi Greg,
>
> It is interesting, so I run the CLR Profiler
>
> http://msdn.microsoft.com/netframework/downloads/tools/default.aspx
>
> You can see the heap there, you can filter to show when a new object is
> loaded on the heap, after creating an instance of the struct and populating
> the string nothing appared on the heap. Maybe this means good news for you.
> Give it a try and keep me posted if you find anything interesting.
>
> My email is Salvapat***@yahoo.co.uk, is good to have people with good
> knowledge around.
>
> cheers
> Salva
>
>
> "gmccallum" wrote:
>
> > What you state is true, you are describing the concept of stack frames.
> >
> > However the c# documentation states that classes are stored on the heap
> > which would mean in the current stack frame would exist a pointer to a memory
> > block on the heap which would contain the actual data.  A struct however
> > (when it contains only value types) would be totally stored on the stack with
> > no pointers to the heap.  Which would be a major speed enhancement.
> >
> > The problem I see is that the moment you create a reference type within the
> > struct then one of three things could occur:
> > 1) the entire struct would be relocated to heap with a pointer placed in the
> > stack frame to hold its location.  (BAD)
> > 2) the struct would remain on the stack with a pointer used to contain the
> > address of the actual string data which has be placed on the heap. (SO SO)
> > 3) the struct would remain on the stack and all the variables defined within
> > would remain entirely on the stack as well.  (no far pointers - EXCELLANT).
> >
> > I am beginning to think that option 2 is what happens, but I was hoping for
> > option 3.
> >
> > -Greg McCallum
> >
Author
12 Apr 2005 4:04 PM
Reginald Blue
gmccallum wrote:
> General Info:
> A struct is stored on the stack and a class on the heap.

This isn't precisely correct.  When you create a stack in a function, it's
(usually) on the stack, but it's possible to copy it to the heap, which is
what happens when you put it in a container class.

> A struct is a value type while a class is a reference type.

This is correct.

> Question:
> What if a struct contains a string property(variable).  The string
> would
> be a reference type being contained in a value type.  Would this
> filter
> up and cause the stack to now be a reference type placed on the heap

no.

> or would it still be on the stack with a pointer used internally to
> point
> to a preallocated string object on the heap?

Yes, although, I disagree with the use of the word "preallocated" there.
There's nothing preallocated.  It's just some memory on the heap for the
string, and every reference to it is, effectively, a pointer, including the
one in the struct.

> The reason for the question is because almost all examples related to
> stacks and discussing the speed benefits never show anything in the
> stack other than value types (which a internally structs themselves
> like int).

Ultimately, this has to to with garbage collection, which a struct will not,
in some cases, be subject to.

> I am creating a large binary tree and currently use classes to
> implement it. There are no problems with the code, but if I could see
> a major performance gain by switching the classes to structs I would
> do it.

I do not think you will see a speed improvement by switching to structs.
You may experience a degradation.  Generally, however, one must test to be
sure, especially in the area of performance, because so many factors can
come into play that you may not know about.

--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone.  My wish has come true.  I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]
Author
12 Apr 2005 4:58 PM
Jon Skeet [C# MVP]
gmccallum <gmccal***@discussions.microsoft.com> wrote:
> General Info:
> A struct is stored on the stack and a class on the heap.

No, it's not that simple.

See http://www.pobox.com/~skeet/csharp/memory.html

(Salvador's already quoted most of it, but it's better to see the whole
thing in context.)

> A struct is a value type while a class is a reference type.

That is certainly true.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

AddThis Social Bookmark Button