背景:
在写一个 链表 相关的题目时犯的错误。
报错内容:
Line 70: Char 15: runtime error: member access within misaligned address 0xbebebebebebebebe for type 'struct ListNode', which requires 8 byte alignment (ListNode.c)
0xbebebebebebebebe: note: pointer points here
<memory cannot be printed>
错误原因:
申请空间之后,没有给结构体内的指针赋值。举例如下:
结构体定义:
struct ListNode {
int val;
struct ListNode *next;
};
错误写法(错误之处并不是语句错误,而是错在不完整):
struct ListNode * head = (struct ListNode *)malloc(sizeof(struct ListNode));
解决方法:如下(申请之后对结构体内的指针赋值,赋值为NULL总没问题)
struct ListNode * head = (struct ListNode *)malloc(sizeof(struct ListNode));
//head->val=0;
head->next=NULL;
写在后面
写代码时要注意细节,要写得完整!切记!切记!
本文介绍了在LeetCode上解决链表问题时遇到的runtime error,错误源于结构体内指针未正确初始化。通过分析错误原因,提出了解决方案——在申请空间后对结构体指针进行赋值,强调编程时注重细节的重要性。
2310





