member access within misaligned address 0x0000002c3931 for type 'struct ListNode‘
From MWeb
在做leetcode 第2题时使用C语言编写链表时报错
错误复现
报错时的代码如下
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
if(l1==NULL) return l2;
if(l2==NULL) return l1;
struct ListNode* result=(struct ListNode*)malloc(sizeof(struct ListNode));
int tmp=l1->val+l2->val;
if(tmp<10){
result->val = tmp;
result->next = addTwoNumbers(l1->next,l2->next);
}else{
result->val = tmp%10;
struct ListNode* tmpN=(struct ListNode*)malloc(sizeof(struct ListNode));
tmpN->val=tmp/10;
result->next = addTwoNumbers(addTwoNumbers(l1->next,l2->next),tmpN);
}
return result;
}
运行后报错
member access within misaligned address 0x000000000e91 for type 'struct ListNode', which requires 8 byte alignment (ListNode.c)
0x000000000e91: note: pointer points here
错误原因
在程序倒数第6行处申请了一个tmpN指向的结构体ListNode空间,而该结构体中包含next指针,若该节点作为整个链表的最后一个节点,如l1和l2分别指向[5]和[6]时,此时tmpN指向的空间并没有初始化next指针,因此报错。
解决办法
使用NULL初始化next指针指向的内容,如
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
if(l1==NULL) return l2;
if(l2==NULL) return l1;
struct ListNode* result=(struct ListNode*)malloc(sizeof(struct ListNode));
int tmp=l1->val+l2->val;
if(tmp<10){
result->val = tmp;
result->next = addTwoNumbers(l1->next,l2->next);
}else{
result->val = tmp%10;
struct ListNode* tmpN=(struct ListNode*)malloc(sizeof(struct ListNode));
tmpN->next=NULL;
tmpN->val=tmp/10;
result->next = addTwoNumbers(addTwoNumbers(l1->next,l2->next),tmpN);
}
return result;
}
By JZ
Less is more
member access within misaligned address 0x0000002c3931 for type 'struct ListNode‘的更多相关文章
- member access within misaligned address 0x000000000031 for type 'struct ListNode', which requires 8 byte alignment
在做LeetCode的two sum题目的时候,提交代码遇到了如题的runtime error,后来在一篇博客上看到了解决方法. 现有如下结构体: struct ListNode { int val; ...
- 141. 环形链表 LeetCode报错:runtime error: member access within null pointer of type 'struct ListNode'
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- leetcode 编译问题:Line x: member access within null pointer of type 'struct TreeNode'
参考: LEETCODE 中的member access within null pointer of type 'struct ListNode' 解决 leetcode 编译问题:Line x: ...
- QT编译错误:member access into incomplete type 'QMouseEvent'
想在QT程序中使用鼠标事件,添加重载的响应函数,并实现后,一直提示 member access into incomplete type 'QMouseEvent' 既然使用了QMouseEvent类 ...
- Protected Member Access
https://msdn.microsoft.com/en-us/library/bcd5672a.aspx 官方的说法The protected keyword is a member access ...
- Access Violation at address 00000000.Read of address 00000000 解决办法
是数组越标或没有初始化某个对象之类的问题,搂住细细检查一下代码, 使用指针前未做检查,而这个指针未初始化. 可能是new后没有delete,这样出现溢出的可能性比较大 检查代码或者跟踪试试 使 ...
- Navicat 提示 Access violation at address ***(如004ECCF4) in module ‘navicat.exe’. Read of address ***(如00000048)
Navicat 提示 Access violation at address ***(如004ECCF4) in module ‘navicat.exe’. Read of address ***(如 ...
- 解决 “access violation at address xxxxxxxxx”错误
在进行磁盘整理的时候,打开Foxmail的时候出现了“access violation at address32383137”错误 和“access violation at address00000 ...
- BCB Access violateion at Address 0000 0003. Read of address 0000 0003
来自网页:(我的电脑做不到) 运行一个程序,莫名出现一个对话框:access violation at address 0000.. read of address000试了几次问题依旧,网上搜了下解 ...
随机推荐
- @Secured()、 @PreAuthorize() 、 @RolesAllowed()
在Spring security的使用中,为了对方法进行权限控制,通常采用的三个注解,就是@Secured().@PreAuthorize().@RolesAllowed(). 示例,修改用户密码必须 ...
- maven与ide工具的整合
maven与myeclipse的整合 1 点击window会出现 2>选择 preferences
- windows下php使用zerophp
官网地址:http://zeromq.org/ 下载windows版本安装(不过php可以不用安装,直接使用扩展包就可以了) 然后下载php的zmq扩展包:https://pecl.php.net/p ...
- Jquery异步 Deferred Object
Deferred Object )); return dtd.promise();};//使用$.when()为普通操作添加回调函数 为多个操作指定回调函数//$.when(deferred, def ...
- 12 Things Developers Will Love About Oracle Database 12c Release 2
by Chris Saxon-Oracle It's Here: Oracle Database 12c Release 2 (12.2) Is available on Oracle Cloud. ...
- 【matlab】 拉格朗日插值
第一个函数 "lagrange1.m" 输入:X Y 与点x0 输出:插值函数对应函数值 y0 function y = lagrange1(X,Y,x0) n = length ...
- libc.so.6: cannot open shared object file: No such file or diretory
环境 centos6.6. 由于误操作 删除了 rm -f /lib64/libc.so.6 导致其他命令不能使用 解决方法: /sbin/sln /lib64/libc-
- 2018.08.30 21:12 第一个Django程序完成
from django.http import HttpResponse def hello(request): return HttpResponse("Hello world ! &qu ...
- eclipse导入maven工程missing artifact(实际是存在的)错误解决
找到出错的jar包文件位置,删掉_maven.repositories文件(或用文本编辑器打开,将“>main=”改为“>=”,即删除main,当然main也可能是其他值),然后updat ...
- Spring Boot Mock单元测试学习总结
单元测试的方法有很多种,比如使用Postman.SoapUI等工具测试,当然,这里的测试,主要使用的是基于RESTful风格的SpringMVC的测试,我们可以测试完整的Spring MVC流程,即从 ...