c++ dynamic memory allocation using “new”

后端 未结 4 1946
温柔的废话
温柔的废话 2020-12-19 16:24

I\'m new to C++, trying to learn by myself (I\'ve got Java background).

There\'s this concept of dynamic memory allocation that I can assign to an array (for example

4条回答
  •  萌比男神i
    2020-12-19 17:12

    For static allocation, you must specify the size as a constant:

      MyObj  arrObject[5];
    

    For dynamic allocation, that can be varied at run-time:

      MyObj  *arrObject = new MyObj[n];
    

    The different between new and malloc is that new will call the ctor for all those objects in the array, while malloc just gives you raw memory.

提交回复
热议问题