1. 程式人生 > >【轉】leetcode錯誤提示:member access within misaligned address 0x000000000031 for type 'struct ListNode'

【轉】leetcode錯誤提示:member access within misaligned address 0x000000000031 for type 'struct ListNode'

錯誤提示:

member access within misaligned address 0x000000000031 for type 'struct ListNode', which requires 8 byte alignment

原因分析:

在連結串列中,連結串列節點定義如下:

  1. Definition for singly-linked list.

  2. * struct ListNode {

  3. * int val;

  4. * struct ListNode *next;

  5. * };

在申請空間時程式碼如下:

temp1=(struct ListNode*)malloc(sizeof(struct ListNode));

由於結構體記憶體在next指標,而申請結構體空間後同時定義了next指標,此時next指標未指向任何空間,故在測試時可能導致上述錯誤。

解決方法為:

增加程式碼使next指標指向空。

temp->next=NULL;