Where are temporary object stored?

后端 未结 4 1137
一个人的身影
一个人的身影 2020-12-01 12:52

IMO temporary objects are stored in dynamic (heap) memory, but I\'m not sure. Can you please confirm or deny my thoughts?

4条回答
  •  旧巷少年郎
    2020-12-01 13:22

    Most (if not all) implementations store them on the stack (i.e. automatic storage), although I don't think the standard mandates anywhere. It's certainly easier to do it like that, as the compiler has to guarantee the temporary variable's life time, and it is possible that said lifetime will encompass a recursive call of the same function, creating another instance of the temporary variable.

提交回复
热议问题