簡體   English   中英

復制結構指針到指針

[英]Copying for struct pointer to pointer

我很好奇為什么這樣的代碼會產生段錯誤

square->type=start.type

它們是相同類型的結構,square是指針,start不是。 我認為start.type中的值將被復制到正方形指向的類型部分。

有人可以解釋段錯誤背后的原因嗎?

這可能是以下原因之一:

  1. square為空。
  2. square不是空值,而是未初始化的,表示它沒有指向有效的對象。
  3. 在執行該行之前刪除了square ,這也意味着它沒有指向有效的對象。
  4. type的重載賦值運算符出了點問題(如果type是一個類/結構並且其賦值運算符被重載)。
  5. start是一個參考,並通過將null遞減來分配,例如:

     Apple *apple=0; Apple &start = *apple; 
  6. 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM