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 13 2265
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
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
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
>> 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
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
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
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
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
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
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]
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Casper |
last post by:
I've been told that structs, being value types, always reside on the
stack and thus need not be deleted manually but simple go out of scope
in due time.
I have also read that all C++ *new*...
|
by: Jasper Kent |
last post by:
Can anyone explain the logic behind structs being allowed neither memeber
initialisers or default constructors.
Doesn't this just encourage developers to create constructors with dummy...
|
by: Zach |
last post by:
When it is being said that, "value types are created on the stack or inline as part of an
object". If a value type is created in an object, and that object is being
called, the value type in that...
|
by: Neil |
last post by:
Hi ,
Quick quetion to help me understand vb.net a bit more.
Why would you use structs as I cant see advantage of using them as I would
always use class. I think that struct go on the stack...
|
by: Christian Christmann |
last post by:
Hi,
I'd like to store structs on an STL stack.
Here is a piece of my code:
#inclue <stack>
...
struct storeInfo {
|
by: Daniel Rudy |
last post by:
What is the difference between packed and unpacked structs?
--
Daniel Rudy
Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m
Why...
|
by: veera sekhar kota |
last post by:
I read structs are stored at stack area or inline-heap based objects.
What is meant by inline-heap based objects? I didnt get that.
Thanks,
Veera.
|
by: The Cool Giraffe |
last post by:
One thing i do know for sure. When one creates a CPP file,
one needs to include the H file. Now, having said that, i
wonder if there are some general hints, requirements or
standard guide lines on...
|
by: Dom |
last post by:
I'm really confused by the difference between a Struct and a Class?
Sometimes, I want just a group of fields to go together. A Class
without methods seems wrong, in that it carries too much...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |