Note that there are some explanatory texts on larger screens.

plurals
  1. PODilemma with using value types with `new` operator in C#
    primarykey
    data
    text
    <p>When <code>operator new()</code> is used with reference type, space for the instance is allocated on the heap and reference variable itself is placed on the stack. Besides that, everything within the instance of reference type, that is allocated on the heap, is zeroed-out.<br> For example here is a class: </p> <pre><code>class Person { public int id; public string name; } </code></pre> <p>In the following code:</p> <pre><code>class PersonDemo { static void Main() { Person p = new Person(); Console.WriteLine("id: {0} name: {1}", p.id, p.name); } } </code></pre> <p><code>p</code> variable is on the stack and the created instance of <code>Person</code> (all of its memebers) is on the heap. <code>p.id</code> would be <code>0</code> and <code>p.name</code> would be <code>null</code>. This would be the case because everything allocated on the heap is zeroed-out.</p> <p>Now what I'm confused about is if I'm using a value type with <code>new</code> operator. For example, take into consideration following structure:</p> <pre><code>struct Date { public int year; public int month; public int day; } class DateDemo { static void Main() { Date someDate; someDate= new Date(); Console.WriteLine("someDate is: {0}/{1}/{2}", someDate.month, someDate.day, someDate.year); } } </code></pre> <p>Now I would like to know what do the following lines from main do:</p> <pre><code> Date someDate; someDate= new Date(); </code></pre> <p>In first line <code>someDate</code> variable is allocated on the stack. Precisely 12 bytes.<br> <strong>My question</strong> is what happens on the second line? What does <code>operator new()</code> do? Does it only zero-out members of <code>Date</code> structure or it allocates space on the heap as well? On one side I wouldn't expect <code>new</code> to allocate space on the heap, of course because in the first line memory is already allocated on the stack for the structure instance. On the other hand, I would expect <code>new</code> to allocate space on the heap and return address of that space, because that's what <code>new</code> should do. Maybe this is because I'm coming from C++ background. </p> <p>Nevertheless if the answer is: "when <code>new</code> is used with value types, it only zeroes-out members of object", than it's a bit inconsistent meaning of <code>new</code> operator because:</p> <ol> <li>when using <code>new</code> with value types, it only zeroes-out members of object on the stack</li> <li>when using <code>new</code> with reference types, it allocates memory on the heap for the instance and zerous-out it's members</li> </ol> <p>Thanks in advance,<br> Cheers</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload