[英]Copying for struct pointer to pointer
我很好奇为什么这样的代码会产生段错误
square->type=start.type
它们是相同类型的结构,square是指针,start不是。 我认为start.type中的值将被复制到正方形指向的类型部分。
有人可以解释段错误背后的原因吗?
这可能是以下原因之一:
square
为空。 square
不是空值,而是未初始化的,表示它没有指向有效的对象。 square
,这也意味着它没有指向有效的对象。 type
的重载赋值运算符出了点问题(如果type
是一个类/结构并且其赋值运算符被重载)。 start
是一个参考,并通过将null递减来分配,例如:
Apple *apple=0; Apple &start = *apple;
start
是对某事物的引用,并且该事物已被删除,例如:
Apple *apple=new Apple(); Apple &start = *apple; delete apple; square->type = start.type; // this line may not cause segfault but this is actually undefined behavior
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.