1

Today i was reading a SO thread about array and its memory allocation. And i found an answer which was neatly explained and i must say its excellent.

But after reading that answer, i got few more questions which i kept asking myself about what i just read. So far still i am unable to answer it myself nor able to google it up. Hence i require your kind help in explaining those questions to me.

  1. Why is array getting created on heap when every of its content is getting stored on stack? Just 2 avoid boxing in case of value types?
  2. If above was true, then why array had 2 be created on heap first of all?

  3. When he said in his answer new int[100] is actually getting created on heap, is 400 (100 * 4) bytes getting allocated on heap? If so, why? because all the values are getting stored on stack

  4. If 1000 item array is created, then how on earth stack is enough to be stored? I know 1 MB is the size of the stack allocated. but in this case it will exceed. So whats the funda??

Please feel free to add your own questions or more info if you may have to this.

Thanks

Community
  • 1
  • 1
Zenwalker
  • 1,883
  • 1
  • 14
  • 27
  • Please see recent duplicate [How are arrays created and accessed](http://stackoverflow.com/questions/7793808/how-are-arrays-created-and-accessed) – Anthony Pegram Oct 20 '11 at 05:07
  • Even thought the topic seems duplicate, i was more into extending the discussion on it as per my doubts. Yes i read the erics blog, still i am unable to get convinced about my doubts. Hence this thread. – Zenwalker Oct 20 '11 at 05:41

2 Answers2

3

When we say array is allocated on heap it means the values will be in heap. Array values are not stored on stack. I think this answers all 3 questions.

var myArray = new int[10];

Above line creates myArray variable on stack but the memory of array is allocated on heap and so all values stored in it also go in heap.

Muhammad Hasan Khan
  • 34,648
  • 16
  • 88
  • 131
  • but if you check that answer, he says values are stored on stack. And if value type values are stored on heap, doesnt it requires boxing? Its bad right. – Zenwalker Oct 20 '11 at 05:10
  • @zenwalker - You're missing the point. when i say int[] test; I declare a variable on the stack capable of holding a !reference! to an array of integers. If i follow that statement up with test = new int[10], the new operator reserves a block of memory on the heap capable of holding 10 integers and returns a reference to this block which then is assigned to the local variable test. Therefore i assign a value to the variable test which is a reference to a location in the Heap which in turn holds an array for 10 integers – Polity Oct 20 '11 at 05:44
  • ok, i got that point but still the values which gets stored as array content are allocated in heap. So dont you think a box is needed? Also please see my comment on SimonC post. Thanks :) – Zenwalker Oct 20 '11 at 05:46
  • 2
    @zenwalker - Boxing is a different proces. With boxing, you want to be able to reference an integer that resides on the stack so you allocate some space on the heap to hold a copy of the value of the variable you want to reference. Thats why boxing is heavy since it needs to allocate and deallocate small objects on the heap. Creating a array of integers if you will on the heap, means that those integers are already there, on the heap and no boxing is required unless you want to reference a individual integer from the array – Polity Oct 20 '11 at 06:00
  • 2
    @zenwalker - that said, lets look at a pracital example. if (myarray[10] == 5). In this case, the local variable myarray (if initiated right) holds a reference to a block of memory on the heap (array) holding the int values. When you say myarray[10] you can think of the following happening. int tempOnStack = myarray[10]. Right a copy of the value of the location myarray[10] is created and assigned to a local variable. Again no boxing is going on since no new allocations are made on the heap – Polity Oct 20 '11 at 06:04
  • 2
    @zenwalker - Lastly, when you assign a value to an element within an array, the same thing happens: myarray[10] = 8; means that the value 8 is written on the location in the heap at myarray[10] which for simplicity can be interpreted as the address within the reference of the variable myarray + (10 * sizeof(int)) – Polity Oct 20 '11 at 06:06
  • Ahhhhhh, i got the point now why no boxing is required. great thanks :) – Zenwalker Oct 20 '11 at 06:06
2

From what I understand, in a non-formal definition, value types are stored locally to where they are defined. So if a single value type is defined in a method, it will be stored on the stack. If it's defined as field on a class, or in this case, as items in an array, then the 'parent' object is on the heap so the value type will also be stored on the heap.

UPDATE

For more details on value types versus reference types and the stack, check out Eric Lippert's articles:

SimonC
  • 6,590
  • 1
  • 23
  • 40
  • Even after reading his blog i am kinda not satisfied or i may be stupid in not getting his technical level. So still i ended up having following questions if we assume that value types getting stored on a heap, does it not required to be boxed? Say in case of int[100] array? How about when i pass int[2000] items as an argument? I guess till the run time just passes it as reference right? What happens in case of params method? If i pass Method(1,2,3,....5000) to it? Still same way? – Zenwalker Oct 20 '11 at 05:42
  • @zenwalker: As I pointed out in another comment, you have it backwards. It is not that "a value type on the heap must be boxed". It is "a value type that is boxed must be on the heap". A value type on the heap might be boxed or it might not be. A value type on the stack is never boxed. Why do you have the false believe that a value type on the heap must be boxed? I like to learn what caused people to believe false things. – Eric Lippert Oct 20 '11 at 07:32