您的计算需要哪些数据?你可以创建一个新的结构来保存你想要的数据吗:
struct WorkingData
{
//data needed for calculations
}
struct CKTcircuit source = //whatever;
struct WorkingData work;
CKTcircuit_populateWorkingData(source, &work);
calculate(&work);
或者,将 aCKTcircuit
用于工作数据,但要小心克隆包含指针的结构。
struct CKTcircuit source = //whatever;
struct CKTcircuit work;
CKTcircuit_populateWorkingData(source, &work);
calculate(&work);
但是 100 个成员并不是世界末日:
可能只需要咬紧牙关,了解规则并使用以下方法进行深度克隆:
注释成员以了解每个结构是浅克隆 OK,还是需要深度克隆。
struct CKTcircuit
{
int x; //shallow clone OK
int *x2; //pointer - needs deep clone
struct Y y; //shallow clone OK iff members of struct Y are all shallow clone OK
struct Y *y2; //pointer type, needs deep clone
} //conclusion for this stuct - needs deep clone
struct CKTcircuit CKTcircuit_clone(struct CKTcircuit *source)
{
struct CKTcircuit result = *source; //shallow clone
//that has dealt with all value types e.g. ints, floats and
//even structs that aren't pointed to and don't contain pointers
//now need to call similar clones functions on all the pointer types
result.pointerX = &x_clone(source->pointerX);
return result;
}
如果这样的东西不存在,您可能需要自定义释放方法来释放内存。
void CKTcircuit_free(struct CKTcircuit *source)
{
x_free(source->pointerX);
free(*source);
}