[英]error: member access with misaligned address
I am working on problem 2 in leetcode (Two Sum) and I keep getting this error.我正在处理 leetcode(Two Sum)中的问题 2,并且一直收到此错误。 I don't understand how this reflects what I wrote: here is my code that I wrote, I think it has something to do with the dynamic allocations.
我不明白这如何反映我写的内容:这是我写的代码,我认为它与动态分配有关。 any help would be appreciated.
任何帮助,将不胜感激。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
int GetLength(struct ListNode* list){
int count = 0;
while (list != NULL){
count ++;
list = list->next;
}
return count;
}
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
int count1 = GetLength(l1);
int count2 = GetLength(l2);
struct ListNode* newVal = malloc(sizeof(struct ListNode));
struct ListNode* start;
if (count1 < count2){
int* tmp = l1;
l1 = l2;
l2 = tmp;
int tmpcount = count1;
count1 = count2;
count2 = tmpcount;
}
// updated l1 is longer
int carryflag = 0;
int iteration = 0;
while (l2 != NULL){
iteration ++;
int sum = l1->val + l2->val + carryflag;
printf("%d\n", iteration);
if (iteration = 1){
start = newVal;
}
newVal->val = sum % 10;
if (sum >= 10) carryflag = 1;
else carryflag = 0;
l1 = l1->next;
l2 = l2->next;
newVal->next = malloc(sizeof(struct ListNode));
newVal = newVal->next;
}
while(l1 != NULL){
newVal->next = malloc(sizeof(struct ListNode));
newVal->val = l1->val;
l1 = l1->next;
newVal = newVal->next;
}
return start;
}
If not take into account typos as for example in this statement如果不考虑例如本声明中的拼写错误
if (iteration = 1){
where there is used the assignment operator =
instead of the comparison operator ==
the program contains serious bugs.在使用赋值运算符
=
而不是比较运算符==
的地方,程序包含严重的错误。
In this code snippet在此代码段中
if (count1 < count2){
int* tmp = l1;
l1 = l2;
l2 = tmp;
int tmpcount = count1;
count1 = count2;
count2 = tmpcount;
}
you are assigning a pointer of the type int *
to a pointer of the type ListNode *
您将
int *
类型的指针分配给ListNode *
类型的指针
l2 = tmp;
So the compiler should issue a message that there are used pointers of incompatible types.所以编译器应该发出一条消息,指出使用了不兼容类型的指针。
Also the function can produce a memory leak if empty lists are passed and can be a reason of undefined behavior because for example the last node can be leaved unitialized.此外,如果传递了空列表,function 可能会产生 memory 泄漏,并且可能是未定义行为的原因,因为例如最后一个节点可能会被单独化。
newVal->next = malloc(sizeof(struct ListNode));
newVal = newVal->next;
And if the second list is empty then the pointer start
will not be even itialized.如果第二个列表为空,则指针
start
甚至不会被初始化。
The second while loop ignores the value of carryFlag
.第二个 while 循环忽略了
carryFlag
的值。
In any case the approach is inefficient.在任何情况下,这种方法都是低效的。 There is no any need to count nodes in the two lists.
无需计算两个列表中的节点。
The function can be declared and defined the following way function可以通过以下方式声明和定义
struct ListNode * addTwoNumbers( const struct ListNode *l1, const struct ListNode *l2 )
{
const int Base = 10;
struct ListNode *head = NULL;
struct ListNode **current = &head;
int carryFlag = 0;
while ( carryFlag != 0 || l1 != NULL || l2 != NULL )
{
*current = malloc( sizeof( struct ListNode ) );
int value = ( l1 == NULL ? 0 : l1->val ) +
( l2 == NULL ? 0 : l2->val ) +
carryFlag;
( *current )->val = value % Base;
carryFlag = value / Base;
( *current )->next = NULL;
current = &( *current )->next;
if ( l1 != NULL ) l1 = l1->next;
if ( l2 != NULL ) l2 = l2->next;
}
return head;
}
Variation on @Vlad from Moscow suggested code:莫斯科建议代码的@Vlad变体:
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode * addTwoNumbers(const struct ListNode *l1, const struct ListNode *l2) {
struct ListNode head = {.next = NULL};
struct ListNode *previous = &head;
const int Base = 10;
int carry = 0;
while (l1 || l2 || carry) {
int value = carry;
if (l1) {
value += l1->val;
l1 = l1->next;
}
if (l2) {
value += l2->val;
l2 = l2->next;
}
struct ListNode *current = malloc(sizeof current[0]);
if (current == NULL) {
// TBD code to handle error
return NULL;
}
current->next = NULL;
current->val = value % Base;
carry = value / Base;
previous->next = current;
previous = current;
}
return head.next;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.