[英]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.