.NET Forum / .NET Framework / New Users / July 2006
Is this a value variable?
|
|
Thread rating:  |
LonelywolF - 09 Jul 2006 17:25 GMT Hi , I have a doubt.
Let's say I define the following structure:
structure strTest dim value_1 as decimal dim value_2 as decimal end structure
As the definition, each instance of this structure will take 16+16=32 bytes space, which is conflicting with the value varible definition. As a result, this one is NOT a value variable and it will be stored in managed heap instead of STACK.
Please correct me if I am wrong. Thanks.
 Signature Regards, Lonelywolf
Göran Andersson - 09 Jul 2006 19:40 GMT No, a structure is always a value type.
Quote from MSDN:
"A struct type is a value type that is typically used to encapsulate small groups of related variables, such as the coordinates of a rectangle or the characteristics of an item in an inventory."
There is nothing there about any size limitations, nor have I found anything about any size limitation anywhere.
On the contrary, in this page:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/ht ml/vbtchUseClassStruct.asp
there is a discussion about performance versus structure size, where it says that copying an object reference and copying a 16 byte structure performs about the same. It also says that when the structure gets larger, the performance degrades. If there was a size limit less than 32 bytes, there surely would be a note of if there.
> Hi , I have a doubt. > [quoted text clipped - 11 lines] > > Please correct me if I am wrong. Thanks. Michael D. Ober - 09 Jul 2006 19:43 GMT This is a value variable and is stored on the stack. Size isn't a consideration whether a variable is stored on the stack or in the heap with a pointer on the stack. The compiler can easily adjust for non-uniform size stack elements.
Mike Ober.
> Hi , I have a doubt. > [quoted text clipped - 11 lines] > > Please correct me if I am wrong. Thanks. Cowboy (Gregory A. Beamer) - 09 Jul 2006 22:03 GMT In this case, as you have defined it as a structure, it is a value type and IS stored on the stack, not the heap.
 Signature Gregory A. Beamer
************************************************* Think Outside the Box! *************************************************
> Hi , I have a doubt. > [quoted text clipped - 13 lines] > > Please correct me if I am wrong. Thanks. Jon Skeet [C# MVP] - 09 Jul 2006 23:32 GMT > In this case, as you have defined it as a structure, it is a value type and > IS stored on the stack, not the heap. (This is in reply to Michael's post as well.)
It's stored wherever the variable/expression holds the value. That may be on the heap as part of another object. The over-simplification of "value types are stored on the stack and reference types are stored on the heap" can cause confusion in my experience.
See http://www.pobox.com/~skeet/csharp/memory.html for more on this.
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
LonelywolF - 10 Jul 2006 08:20 GMT Hi Jon,
Actually I asked this because I have read the book "MCTS self-pace training toolkit"...
In it , when describing these terms, they are using this simple term. But there is also stating a condition like "If the size of the instance is not larger than 16 bytes, it should be declared as structure". I just wonder where comes this 16 bytes.
Also, as you described, can I say that as long as the structure is not static, it will be stored in stack.
 Signature Regards, Lonelywolf
> > In this case, as you have defined it as a structure, it is a value type and > > IS stored on the stack, not the heap. [quoted text clipped - 7 lines] > > See http://www.pobox.com/~skeet/csharp/memory.html for more on this. Göran Andersson - 10 Jul 2006 09:03 GMT > Hi Jon, > [quoted text clipped - 5 lines] > larger than 16 bytes, it should be declared as structure". I just wonder > where comes this 16 bytes. The 16 bytes comes from Microsoft, to be exact from the article that I referenced in my previos post in the thread:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/ht ml/vbtchUseClassStruct.asp
I hope that the statement that you quoted is taken from a more comprehensive discussion about value types versus reference types, and performance considerations. Just because the data size is smaller than 16 bytes, it's not at all certain that it's suitable to be a structure, and even if the data size is larger than 16 bytes, it's not certain that it shouldn't be a structure.
> Also, as you described, can I say that as long as the structure is not > static, it will be stored in stack. If a value type is declared as a variable in a method, it will be stored on the stack.
If a value type is declared as a non-static member in a class, it will be stored inside the class instance, on the heap.
If a value type is declared as a static member in a class, I'm not really sure, but I'm pretty sure that it will be stored on the heap. Perhaps someone can give you a definitive answer on that. It doesn't really matter much, though, as it will be allocated only once when the assembly is loaded, and it will not be released until the assembly is unloaded (when the application ends, unless you loaded the assembly dynamically).
Michael D. Ober - 10 Jul 2006 12:58 GMT Valid point. I have discovered that when this particular question comes up, the issue of declaration as a local variable or as part of an object isn't really an issue. Value variables are always stored as an offset to the base of the reference frame, be that on the stack (local variable), global data storage (global variable or static/shared object data), or heap (part of an object's instance).
Mike.
> > In this case, as you have defined it as a structure, it is a value type and > > IS stored on the stack, not the heap. [quoted text clipped - 7 lines] > > See http://www.pobox.com/~skeet/csharp/memory.html for more on this. Jon Skeet [C# MVP] - 10 Jul 2006 16:24 GMT > Valid point. I have discovered that when this particular question comes up, > the issue of declaration as a local variable or as part of an object isn't > really an issue. You must have seen different threads to me then - I've seen plenty of people specifically saying, "If structs are allocated on the stack, which stack is used for members of classes?" (or words to that effect, usually with an example).
It's just another example of an imprecise which is intended to make life simpler actually making things more complicated. The other classic example is "objects are passed by reference by default" (with the context being C#).
> Value variables are always stored as an offset to the base > of the reference frame, be that on the stack (local variable), global data > storage (global variable or static/shared object data), or heap (part of an > object's instance). Agreed.
Jon
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|