When should a class be allocated on the stack instead of the heap

前端 未结 4 556
天命终不由人
天命终不由人 2021-01-11 12:11

In the past whenever I needed to create an instance of a class I would use new to allocate it on the heap (except for stl classes, and math classes like vec3 and mat4).

4条回答
  •  独厮守ぢ
    2021-01-11 12:50

    You allocate on the heap only when you need to dynamically allocate the memory. Meaning you do not know how much you need to allocate at compile time. You allocate on the stack all other times

提交回复
热议问题