By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,567 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,567 IT Pros & Developers. It's quick & easy.

Array Fundamentals

P: n/a
Hello All,

In .Net Array is a reference type and an int is a value type. If I
create an array of int, then will the items inside the array get boxed?

If yes, it will be a terrible overhead. If no, then where will the array
elements exist? since items inside the array are value type they have to
be on the stack of the executing thread, but the array itself will be on
the heap since its a reference type.
Thanks for your help in advance.

regards,
Abhishek.
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
n!
> In .Net Array is a reference type and an int is a value type. If I
create an array of int, then will the items inside the array get boxed?
No, the reference is to the array itself not to the items contained.
If yes, it will be a terrible overhead. If no, then where will the array
elements exist? since items inside the array are value type they have to
be on the stack of the executing thread, but the array itself will be on
the heap since its a reference type.


Value types are copied (by value) into each array element. Value types do
not *have* to exist on the stack they can be copied around freely.

n!
Nov 16 '05 #2

P: n/a
Inline ***

Willy.

"Abhishek Srivastava" <ab*****************@nospam.net> wrote in message
news:u4**************@TK2MSFTNGP09.phx.gbl...
Hello All,

In .Net Array is a reference type and an int is a value type. If I create
an array of int, then will the items inside the array get boxed?
*** No.
If yes, it will be a terrible overhead. If no, then where will the array
elements exist? *** On the heap, as they are elements of an array which is a ref. type.

since items inside the array are value type they have to be on the stack of the executing thread, but the array itself will be on
the heap since its a reference type.
*** Value types can be stack allocated, but they reside on the heap when
they are members/elements of an instance of a ref type.

Thanks for your help in advance.

regards,
Abhishek.

Nov 16 '05 #3

P: n/a

Thanks for your quick help.
*** Value types can be stack allocated, but they reside on the heap
when they are members/elements of an instance of a ref type.
I don't understand this statement. How can a value type exist on the
heap. AFAIK, the main difference between a value type and a reference
type is that only the reference type exist on the heap.

So how come value type exist on the heap in case of arrays?

regards,
Abhishek.

Willy Denoyette [MVP] wrote: Inline ***

Willy.

"Abhishek Srivastava" <ab*****************@nospam.net> wrote in message
news:u4**************@TK2MSFTNGP09.phx.gbl...
Hello All,

In .Net Array is a reference type and an int is a value type. If I create
an array of int, then will the items inside the array get boxed?


*** No.

If yes, it will be a terrible overhead. If no, then where will the array
elements exist?


*** On the heap, as they are elements of an array which is a ref. type.

since items inside the array are value type they have to
be on the stack of the executing thread, but the array itself will be on
the heap since its a reference type.


*** Value types can be stack allocated, but they reside on the heap when
they are members/elements of an instance of a ref type.

Thanks for your help in advance.

regards,
Abhishek.


Nov 16 '05 #4

P: n/a
"Abhishek Srivastava" <ab*****************@nospam.net> wrote in message
news:eQ**************@TK2MSFTNGP11.phx.gbl...

Thanks for your quick help.
*** Value types can be stack allocated, but they reside on the heap
when they are members/elements of an instance of a ref type.


I don't understand this statement. How can a value type exist on the heap.
AFAIK, the main difference between a value type and a reference type is
that only the reference type exist on the heap.

So how come value type exist on the heap in case of arrays?


Instances of "Reference types" can ONLY exist on the heap.
Instances of "Value types" (the bits that make up the value) can exist on
both the stack and the heap.
Consider following sample:
void SomeMethod() {
int[] intArr = new int[] {1, 2, 3};
...
intArr is a local variable, as such it is stack allocated, and it holds a
reference to a heap allocated object of type "int array".
The elements of the array are int's and their values are copied to the heap
allocated object space.

Stack Heap

IntArr ------->|Obj header |
|--------------|
| 1 |
|--------------|
| 2 |
|--------------|
| 3 |
|--------------|

Willy.
Nov 16 '05 #5

P: n/a

Underlying Concepts:
===================

When a value-type object is created, C# allocates a single
space in memory, and puts the contents of the object into
it. Primitive types such as int, float, bool or char are
value types. When the runtime deals with a value type,
it's dealing directly with its underlying data and this
can be very efficient, particularly with primitive types.

With reference types, however, an object is created in
memory, and then handled through a separate reference -
rather like a pointer.

-------

Now as I understand all variables defined under a
reference type (for example class object), are stored on
Heap.

Only local variables of a function are allocated on Stack.

Therefore, the values inside an Array should be allocated
on Heap.

Check following links for further reference:
http://blogs.msdn.com/cbrumme/archiv.../10/51425.aspx

--
Cheers,
Rahul Anand

-----Original Message-----

Thanks for your quick help.
*** Value types can be stack allocated, but they reside on the heap when they are members/elements of an instance of a ref type.

I don't understand this statement. How can a value type exist on theheap. AFAIK, the main difference between a value type and a referencetype is that only the reference type exist on the heap.

So how come value type exist on the heap in case of arrays?
regards,
Abhishek.

Willy Denoyette [MVP] wrote:
Inline ***

Willy.

"Abhishek Srivastava" <ab*****************@nospam.net>

wrote in message news:u4**************@TK2MSFTNGP09.phx.gbl...
Hello All,

In .Net Array is a reference type and an int is a value type. If I createan array of int, then will the items inside the array get boxed?


*** No.

If yes, it will be a terrible overhead. If no, then where will the arrayelements exist?


*** On the heap, as they are elements of an array which is a ref. type.
since items inside the array are value type they have to
be on the stack of the executing thread, but the array itself will be onthe heap since its a reference type.


*** Value types can be stack allocated, but they reside on the heap when they are members/elements of an instance of a ref type.

Thanks for your help in advance.

regards,
Abhishek.


.

Nov 16 '05 #6

P: n/a
Abhishek Srivastava <ab*****************@nospam.net> wrote:
> *** Value types can be stack allocated, but they reside on the heap
> when they are members/elements of an instance of a ref type.


I don't understand this statement. How can a value type exist on the
heap. AFAIK, the main difference between a value type and a reference
type is that only the reference type exist on the heap.


No. That's just an oversimplification which confuses people,
unfortunately.

See 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
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.