By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,698 Members | 1,535 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,698 IT Pros & Developers. It's quick & easy.

General Question About Structs and Stack

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
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

Nov 17 '05 #2

P: n/a
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

Nov 17 '05 #3

P: n/a
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

Nov 17 '05 #4

P: n/a
>> 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" <gm*******@discussions.microsoft.com> wrote in message
news:A3**********************************@microsof t.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

Nov 17 '05 #5

P: n/a
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" <gm*******@discussions.microsoft.com> wrote in message
news:A3**********************************@microsof t.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


Nov 17 '05 #6

P: n/a
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
"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

Nov 17 '05 #7

P: n/a
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
"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" <gm*******@discussions.microsoft.com> wrote in message
news:A3**********************************@microsof t.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


Nov 17 '05 #8

P: n/a
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

Nov 17 '05 #9

P: n/a
Hi Greg,

It is interesting, so I run the CLR Profiler

http://msdn.microsoft.com/netframewo...s/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 Sa*********@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

Nov 17 '05 #10

P: n/a
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]
Nov 17 '05 #11

P: n/a
I will do so. Thanks for the interaction.
-Greg
"Salvador" wrote:
Hi Greg,

It is interesting, so I run the CLR Profiler

http://msdn.microsoft.com/netframewo...s/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 Sa*********@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

Nov 17 '05 #12

P: n/a
Salvador <Sa******@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
Nov 17 '05 #13

P: n/a
gmccallum <gm*******@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
Nov 17 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.