
前言
最近博主在刷力扣时,明明代码逻辑都没问题,但总是报下面这个错误:
runtime error: member access within misaligned address 0xbebebebebebebebe for type 'struct ListNode', which requires 8 byte alignment [ListNode.c]
0xbebebebebebebebe: note: pointer points here
原因和解决办法
原因在于没初始化,赋初值。
例如我们malloc下面这样一个节点:
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* head;
head=(struct ListNode*)malloc(sizeof(struct ListNode));
这样对吗?
由于LeetCode检测机制更加严格,所以我们在创建节点是,还需将指针域赋值。
正确创建节点方式:文章来源:https://www.toymoban.com/news/detail-659370.html
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* head;
head=(struct ListNode*)malloc(sizeof(struct ListNode));
head->next=NULL;
总结
- 问题:创建变量时,没有初始化。
- 解决方法:创建变量后,立即置空或赋初值。
博主再多说一句,上述错误报告仅在LeetCode上出现,在牛客网上没有。
由于两个平台测试机制不同,在此问题上没有谁好谁坏。文章来源地址https://www.toymoban.com/news/detail-659370.html
到了这里,关于runtime error: member access within misaligned address(力扣最常见错误之一)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!