Is this a value variable?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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/d...-us/dv_vstechart/html/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.
 
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.
 
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.

--
Gregory A. Beamer

*************************************************
Think Outside the Box!
*************************************************
 
Cowboy (Gregory A. Beamer) said:
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.
 
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.
 
LonelywolF said:
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.

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/d...-us/dv_vstechart/html/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).
 
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.
 
Michael said:
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
 
Back
Top