General Question About Structs and Stack

G

Guest

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
 
G

Guest

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
 
G

Guest

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)
 
G

Guest

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)
 
J

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.
 
G

Guest

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
 
G

Guest

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
 
G

Guest

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
 
G

Guest

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
 
G

Guest

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 (e-mail address removed), is good to have people with good
knowledge around.

cheers
Salva
 
R

Reginald Blue

gmccallum said:
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]
 

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