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

后端 未结 9 1306
既然无缘
既然无缘 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条回答
  •  天涯浪人
    2020-12-28 09:16

    In Java and C#, we don't need to allocate primitive types on the heap. They can be allocated on the stack ( not that they are restricted to stack ). Whereas, in C++ we can have primitive as well as user defined types to be allocated on both stack and heap.

提交回复
热议问题