473,695 Members | 2,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about value types

When it is being said that, "value types are created on the stack [no
problem with that] or inline [what is being said here?] 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 object, is still created on the stack, I
would say, so I don't understand this inline business. Apart from the fact
that it is my understanding that "inline" as it exists in C++ [e.g. inline
functions] doesn't exist in C#. Could someone please shed some light on this
basic issue for me?
Nov 16 '05 #1
5 2096
Creating instances of value types as local variables creates them on
the stack. If you have a value type as a field of a reference type,
then that is stored as part of the object. It can't be stored on the
stack, as the contents of the stack are valid only till the end of
execution of a method, however reference types outlive that (as they
are created on the heap). The same is the case with an array of value
types, they are stored inline on the heap.

And yes, there is no inline keyword, although the JIT compiler might
inline functions it chooses to.

Regards
Senthil

Nov 16 '05 #2
"sadhu" <se**********@w devs.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .

<snipped a couple of lines>
The same is the case with an array of value types, they are stored inline

on the heap.

Would you please explain the meaning of "inline" here?
Nov 16 '05 #3
I think that "inline" is an unfortunate bit of jargon that crept into
the explanation you read.

Think of value types as types that act (and are allocated) in the same
way as an integer or a float.

If you declare an int local variable in a method somewhere, the space
for that int is allocated on the stack. Similarly, if you declare an
int parameter to a method, the argument is _copied_ onto the stack when
the method is called. Value types act in exactly the same way.

However, if you declare an int as a field of a class, space for that
int is reserved within ("inline", as the description you read
unfortunately expressed it) the space allocated on the heap for each
instance of the class. So, each class instance has some space set aside
for it on the heap, and within that space is space for the int. The
same with value types.

Reference types act differently. All reference types are always
allocated on the heap. References (pointers) to those reference types
are then stored in variables, passed to methods, and stored in object
instances according to the same rules as value types. In effect, the
reference (pointer) to the reference type is the value, whereas for
value types the entire bit pattern for the type is the value.

Arrays (that is, native arrays, not ArrayList or other aggregate
structures) act the way you would expect them to given the above
behaviour. An array of ints (or any other value type) is allocated on
the heap as a block of memory big enough to hold all of the values it
must contain. An array of reference types (object instances) is
allocated as an array of references (pointers) to objects that are
stored elsewhere on the heap.

Finally, an array, being a reference type itself, is stored and passed
around as a reference (pointer).

Nov 16 '05 #4
"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
<snipped>

I compiled the following text - would you agree with this text?

Variables and constants are storage locations with names. A constant is a
variable with a continuous value. A "read only" variable is a constant.
Variables can be classified in terms of location, size, scope, type, value,
duration (the time span during which they have a life) and how their value
is retrieved. Variables can be located on the heap or on the stack. The size
of the storage location is independent of the compiler or the operating
system, but is determined by their type (e.g. int, double). Scope and
duration are determined by where the variable is declared. Variables
declared in a method - or in the parameter list of a method - are local
variables. They have no life outside the method. Parameters can be straight
values, or additionally qualified as "out" (returns a value), or "ref"
(changes the value). The value of a variable can be numerical or
alpha-numerical (i.e. a string value). Variables like int are called
"intrinsic" , or (=) "built-in" or (=) "primitive" types. Primitive types are
structs. Which means that all variable in C# are objects. The value of a
variable is retrieved directly from the stack ("value types"), or indirectly
via a pointer ("reference types"). Pointers represent values, which
themselves cannot be approached ditectly. Because of this difference value
types are more efficient. Pointers are stored on the stack, whereas the
values they represent are stored on the heap. A variable declared in an
object is stored together (some authors say "inline") with that object. For
example a system array refers to a contiguous block of memory on the heap,
large enough to hold the number and type of elements for which it was
declared. Its pointers are on the stack; its values are on the heap. But in
the case of an array of reference types, there are pointers on the stack -
to pointers on the heap - pointing to values on the heap.
Nov 16 '05 #5
Rats. I wrote a reply on Saturday, but evidently it was swallowed by
the Web.
Variables and constants are storage locations with names. A constant is a variable with a continuous value.
Be careful. In C#, constants often do not have storage locations. They
can be (usually are) just compile-time constructs. For example, the
compiler will render

const int a = 5;
const int b = 6;
int c = a + b;

down to this:

int c = 11;

Variables do have storage locations; constants may not.
A "read only" variable is a constant.
Not exactly. The "readonly" keyword in C# guarantees that the variable
can be changed only in the class's constructor(s) and nowhere else.
However, if the "readonly" variable is a reference to an object on the
heap, the "readonly" keyword does _not_ guarantee that the object
itself cannot be changed outside the constructor, only that the
reference will never point to any other object than the one it was
pointing to when the constructor finished executing.

"readonly" built-in types (such as int and float) are in fact
invariable after the constructor terminates, and are a handy way to
create constants that require some complex calculations... constants
that can't simply be built at compile time.

<snip> The value of a variable can be numerical or alpha-numerical (i.e. a string value).

A little simplistic, I think. From the lowest-level point of view,
variables just contain bit patterns that can be interpreted in various
ways. From a higher-level point of view, variables have an almost
infinite variation of types, as the .NET Framework class library
illustrates. As a trivial example, enumerated types don't fall into
either of these categories, and neither do structs that compose complex
value types.
Variables like int are called "intrinsic" , or (=) "built-in" or (=) "primitive" types. Primitive types are structs.
That's backward, to my way of thinking. Primitive types are not
structs; structs act in many ways like primitive (value) types. The two
have similar behaviour, but that doesn't imply an "is a" relationship,
in my mind, and especially not "primitive type is-a struct".
Which means that all variable in C# are objects.
Now, that's an interesting assertion. It all depends upon how you
define "object". I tend to refer to "objects" in C# as reference types,
but then it's true that in C# value types derive from Object and have
methods, including GetType(), so they have some properties of objects.
However, you can't inherit from them. So are they really full-fledged
"objects"? They certainly more object-like than primitive types in,
say, Java or C++.
The value of a variable is retrieved directly from the stack ("value types"), or indirectly via a pointer ("reference types").
For local variables and method arguments, yes.
Pointers represent values, which themselves cannot be approached directly. Because of this difference value types are more efficient.

Be careful here. Yes, value types are "more efficient" for many
operations, but this does _not_ mean that you should decide between
using a value type and using a reference type based on value types
being "more efficient". This is efficiency at a very low (I'd say even
"trivial") level. Real performance gains are usually had by looking at
efficiency at a far higher level of abstraction than this. In that
sense, the "most efficient" construct--value type or reference
type--for a job is the one that best fits the design. Deciding to use
value types becaues of some idea that they are generally "more
efficient" usually leads to tortured and--yes--inefficient code, in my
experience.
Pointers are stored on the stack, whereas the values they represent are stored on the heap.

Again, yes, for reference types that are local variables or method
arguments.
A variable declared in an object is stored together (some authors say "inline") with that object.

Yes. On the heap.
For example a system array refers to a contiguous block of memory on the heap, large enough to hold the number and type of elements for which it was declared. Its pointers are on the stack; its values are on the heap. But in the case of an array of reference types, there are pointers on the stack - to pointers on the heap - pointing to values on the heap.


Yes, keeping in mind that when you say "its pointers" above, you're
talking about the various references that your application is holding
to the array. Those are held on the stack (or, if they form part of an
object's state, on the heap with the other object state data).

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
1753
by: Method Man | last post by:
I understand that arrays and structs can't be passed by value into and out of functions since they can be arbitrarily big. My question is: Why are types allowed to be passed by value? Couldn't my types be arbitraily big as well?
26
1929
by: phoenix | last post by:
Hello, I've got a design question. I need to keep track of some variables and I am planning to put them inside a class or struct. Basically I'm talking about 10 bools, 20 ints and 2 arrays of ints. The size of the arrays would depend on some external value (going from 0 to around 1000 max). I would have an array of max 255 of these classes/structs (in most cases it will be less then 5 however) Since there's no real business logic my...
19
1635
by: Xandau | last post by:
hello all, i wotk with java every day but last time i have interested in C#. everything goes great except one thing... in java everything is a reference (except plain types) so i thought that in C# will be the same - i was wrong.... or if in C# is the same as in java please tell me ;-) I have an ArrayList names ls : // Point is a class System.Draw....
13
2308
by: gmccallum | last post by:
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...
6
1715
by: Mike | last post by:
Lets just say my app is done HOO HOO. Now, I'm accessing the database via a web service and one thing i noticed that my app is running real slow. When I first started working on the app is ran pretty quick returned the data to the screens in about 2 - 3 seconds. Now its going about 5 - 10 seconds. How can I beef it up for better performance.
13
1553
by: cgough | last post by:
My true programming language is C++. I am at best a VB6 hacker that is just getting into VB.NET. I have a quick question about when to new and when not to new. Consider the following 2 classes. In the first I new an integer and assign it to i, in the second one I don't bother. In both cases, an integer is created and I can use it. If I try to use a Collection object without New, I get a NULL reference exception.
20
1909
by: David | last post by:
I feel like an idiot asking this but here goes: I understand the 'concept' of scope and passing data by value and/or by reference but I am confused on some specifics. class example{ int i; //my global var private void method1(int myInt){ int x; //local var etc...
20
4035
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This tells me if a variable has changed, give me the original and current value, and whether the current value and original value is/was null or not. This one works fine but is recreating the same methods over and over for each variable type. ...
17
2316
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the same as its address. The second one simply depends on a term that is not well-defined. Most people consider the type to be an important part of the notion of
0
8555
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9112
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8971
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8817
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5831
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4336
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2994
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2258
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.