Does new always allocate on the heap in C++ / C# / Java

后端 未结 9 1327
既然无缘
既然无缘 2020-12-28 08:58

My understanding has always been, regardless of C++ or C# or Java, that when we use the new keyword to create an object it allocates memory on the heap. I thou

9条回答
  •  猫巷女王i
    2020-12-28 09:24

    In c#, a class always lives on the heap. A struct can be either on the heap or stack:

    • variables (except captures and iterator blocks), and fields on a struct that is itself on the stack live on the stack
    • captures, iterator blocks, fields of something that is on the heap, and values in an array live on the heap, as do "boxed" values

提交回复
热议问题