行业知识
runtime error: member access within misaligned address(力扣最常见错误之一)
2026-11-04 05:27:52
来源:互联网转载
runtime error: member access within misaligned address(力扣最常见错误之一)
- 前言
- 原因和解决办法
- index.php/tags-41973.html" >���结
前言
最近博主在刷力扣时,明明代码逻辑都没问题,但总是报下面这个错误:
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检测机制更加严格,所以我们在创建节点是,还需将指针域赋值。
正确创建节点方式:
struct ListNode { int val; struct ListNode *next; };struct ListNode* head; head=(struct ListNode*)malloc(sizeof(struct ListNode)); head->next=NULL;
总结
- 问题:创建变量时,没有初始化。
- 解决方法:创建变量后,立即置空或赋初值。
博主再多说一句,上述错误报告仅在LeetCode上出现,在牛客网上没有。
由于两个平台测试机制不同,在此问题上没有谁好谁坏。
上一篇:揭秘手机转盘抽奖秘籍,轻松掌握中奖技巧,赢大奖不是梦!
下一篇:免费域名怎么样?