When should I pass or return a struct by value?

前端 未结 8 575
暗喜
暗喜 2020-12-02 17:10

A struct can be either passed/returned by value or passed/returned by reference (via a pointer) in C.

The general consensus seems to be that the former can be applie

8条回答
  •  隐瞒了意图╮
    2020-12-02 17:28

    in an abstract way a set of data values passed to a function is a structure by value, albeit undeclared as such. you can declare a function as a structure, in some cases requiring a type definition. when you do this everything is on the stack. and that is the problem. by putting your data values on the stack it becomes vulnerable to over writing if a function or sub is called with parameters before you utilize or copy the data elsewhere. it is best to use pointers and classes.

提交回复
热议问题