【转】leetcode错误提示:member access within misaligned address 0x000000000031 for type 'struct ListNode'

转载地址:https://blog.csdn.net/lv1224/article/details/54675703

错误提示:

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;

猜你喜欢

转载自blog.csdn.net/y___y___/article/details/81592218