Array Fundamentals

  • Thread starter Abhishek Srivastava
  • Start date
A

Abhishek Srivastava

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.
 
N

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!
 
W

Willy Denoyette [MVP]

Inline ***

Willy.

Abhishek Srivastava said:
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.
 
A

Abhishek Srivastava

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.
 
W

Willy Denoyette [MVP]

Abhishek Srivastava said:
Thanks for your quick help.


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.
 
R

Rahul Anand

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/archive/2003/05/10/51425.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top