したがって、私は leetcodeの問題番号2 を計算しています(基本的に、リストで2つの数値が逆になり、それらを合計して、逆のリストで答えを返す必要もあります)。しかし、この厄介なエラーが発生し続けます:「実行時エラー:8バイトのアライメントが必要なタイプ「struct ListNode」のミスアライメントアドレス0x000000000031内のメンバーアクセス」。私は自分のマシンでそれを実行し、それは期待通りの結果を与えて、うまく働きます。私のコード:
/*Definition of the list:
struct ListNode {
int val;
struct ListNode* next;
};*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode* answer = malloc(sizeof(struct ListNode));
answer->next = NULL;
answer->val = 0;
int auxInt = 0;
struct ListNode* auxVar = answer;
while(l1 != NULL || l2 != NULL) {
if(!l1) {
auxInt = answer->val;
answer->val = (auxInt + l2->val) % 10;
if(((l2->val + auxInt) / 10) != 0) {
answer->next = malloc(sizeof(struct ListNode));
answer->next->val = (l2->val + auxInt) /10;
}
l2 = l2->next;
}
else if(!l2) {
auxInt = answer->val;
answer->val = (auxInt + l1->val) % 10;
if(((l1->val + auxInt) / 10) != 0) {
answer->next = malloc(sizeof(struct ListNode));
answer->next->val = (l1->val + auxInt) /10;
}
l1 = l1->next;
} else {
auxInt = answer->val;
answer->val = (l1->val + l2->val + auxInt) % 10;
if(((l1->val + l2->val + auxInt) / 10) != 0) {
answer->next = malloc(sizeof(struct ListNode));
answer->next->val = (l1->val + l2->val + auxInt) /10;
}
l1 = l1->next;
l2 = l2->next;
}
if(l1 == NULL && l2 == NULL)
break;
else
{
if(!answer->next)
answer->next = malloc(sizeof(struct ListNode));
answer = answer->next;
answer->next = NULL;
}
}
return auxVar;
}
この問題の原因は何ですか?御時間ありがとうございます。
編集:クラッシュの原因となる数値を含む検証可能な例を次に示します。
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
/*typedef struct InvertedList {
int val;
struct InvertedList *next;
struct InvertedList *previous;
}Lista;*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode* answer = malloc(sizeof(struct ListNode));
answer->next = NULL;
answer->val = 0;
int auxInt = 0;
struct ListNode* auxVar = answer;
while(l1 != NULL || l2 != NULL) {
if(!l1) {
auxInt = answer->val;
answer->val = (auxInt + l2->val) % 10;
if(((l2->val + auxInt) / 10) != 0) {
answer->next = malloc(sizeof(struct ListNode));
answer->next->val = (l2->val + auxInt) /10;
}
l2 = l2->next;
}
else if(!l2) {
auxInt = answer->val;
answer->val = (auxInt + l1->val) % 10;
if(((l1->val + auxInt) / 10) != 0) {
answer->next = malloc(sizeof(struct ListNode));
answer->next->val = (l1->val + auxInt) /10;
}
l1 = l1->next;
} else {
auxInt = answer->val;
answer->val = (l1->val + l2->val + auxInt) % 10;
if(((l1->val + l2->val + auxInt) / 10) != 0) {
answer->next = malloc(sizeof(struct ListNode));
answer->next->val = (l1->val + l2->val + auxInt) /10;
}
l1 = l1->next;
l2 = l2->next;
}
if(l1 == NULL && l2 == NULL)
break;
else
{
if(!answer->next)
answer->next = malloc(sizeof(struct ListNode));
answer = answer->next;
}
}
return auxVar;
}
void adicionaLista(struct ListNode* ptrLista, char *array, int size) {
struct ListNode *auxNode = ptrLista;
for(int i = 0; i < size; i++) {
ptrLista->val = array[i];
if(i + 1 != size) {
ptrLista->next = malloc(sizeof(struct ListNode));
ptrLista = ptrLista->next;
}
}
ptrLista = auxNode;
printf("\n");
}
void printAnswer(struct ListNode *ptrAnswer) {
for(; ptrAnswer != NULL; ptrAnswer = ptrAnswer->next)
printf("%d ", ptrAnswer->val);
}
int main()
{
struct ListNode *l1 = malloc(sizeof(struct ListNode));
struct ListNode *l2 = malloc(sizeof(struct ListNode));
char lista[9] = {4,5,2,2,9,3,8,9,2};
char lista2[9] = {0,7,6,1,6,5,0,6,7};
adicionaLista(l1, lista, 9);
adicionaLista(l2, lista2, 9);
struct ListNode *answer = addTwoNumbers(l1, l2);
printAnswer(answer);
return 0;
}
主な問題は、最後の要素のnext
がNULL
に設定されていないことです。
二次的な問題は...
printf("\n");
は、データの追加関数に含めないでください。出力関数で使用する必要があります。修正および縮小されたコード
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) {
struct ListNode anchor = { .next = NULL }, *curr = &anchor;
int carry = 0;
while(l1 != NULL || l2 != NULL || carry) {
int val1 = 0, val2 = 0;
if(l1) {
val1 = l1->val;
l1 = l1->next;
}
if(l2) {
val2 = l2->val;
l2 = l2->next;
}
int answer = val1 + val2 + carry;
carry = answer > 9;//val1 and val2 are one digit.
curr = curr->next = malloc(sizeof(struct ListNode));
curr->val = answer % 10;
curr->next = NULL;
}
return anchor.next;
}
struct ListNode *makeListFromChars(const char *array, int size) {
struct ListNode anchor = { .next = NULL }, *curr = &anchor;
for(int i = 0; i < size; i++) {
curr = curr->next = malloc(sizeof(struct ListNode));//Creating lists and adding data is a separate function.
curr->val = array[i];
curr->next = NULL;
}
return anchor.next;
}
void printList(struct ListNode *p) {
for(; p; p = p->next)
printf("%d ", p->val);
printf("\n");
}
int main(void){
char lista1[9] = {4,5,2,2,9,3,8,9,2};
char lista2[9] = {0,7,6,1,6,5,0,6,7};
struct ListNode *l1 = makeListFromChars(lista1, 9);
struct ListNode *l2 = makeListFromChars(lista2, 9);
printList(l1);
printList(l2);
struct ListNode *answer = addTwoNumbers(l1, l2);
printList(answer);
//freeList(l1);freeList(l2);freeList(answer);
return 0;
}