473,320 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Array Fundamentals

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
6 1290
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
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

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
"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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
1
by: Piotr Galuszkiewicz | last post by:
Hi! I'm looking for 6. edition of "Core Java 2 vol.1 - Fundamentals" (authors: Cay Horstman, Gary Cornell). I want to buy back used book from somebody. BUT(!) I'm looking for new, 6. edition...
0
by: Frank Luff | last post by:
Does anyone have any experience with or recommendations on Oracle's CDROM for OCP DBA Fundamentals I? $600 is a lot to shell out sight unseen. Thanks for any comments. Frank
5
by: Marcel Hug | last post by:
Hi NG ! I'm new in C# and I'm reading a book about the fundamentals and concepts. In the chapter Methods it's written to use virtual, if i would like to override the method in a subclass. This...
1
by: vidu13 | last post by:
hi, I am going to appear for Exam# 1Z1-221:Oracle Inventory Management 11i Fundamentals which is the beta version. Is there anyone who has appeared for these exams who can give some tips on...
8
by: michael sorens | last post by:
I have written a few applications to read data from a database but now, as I turn my attention to writing to a database, I am apparently missing some fundamentals. I created a new database with...
0
by: nkechifesie | last post by:
Am reading for Fundamentals1 exam , would love to take it this month. Thats Oracle 9i fundamentals i and was wondering if there are any advice from you all.
2
by: situ | last post by:
Hi, i took db2 fundamentals 730 exam on Dec-06 in one of the prometric centers, and succesfully passed also. but the problem is i didn't got my certificate yet, usually how long it takes to...
19
by: Adam | last post by:
Hi, I'd like to return an (arbitrary length) string array from a function so that after calling the array I've got a list of strings I can access. After much perusing of the internet I found a...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.