runtime error: member access within misaligned address (one of the most common errors in Lituo)

runtime error: member access within misaligned address (one of the most common errors in Lituo)


insert image description here

foreword

Recently, when bloggers are swiping force buttons, it is clear that the code logic is fine, but they always report the following error:

 runtime error: member access within misaligned address 0xbebebebebebebebe for type 'struct ListNode', which requires 8 byte alignment [ListNode.c]
0xbebebebebebebebe: note: pointer points here

Causes and Solutions

The reason is that it is not initialized and assigned an initial value.
 
For example, we malloc the following node:

 struct ListNode {
    
    
     int val;
     struct ListNode *next;
 };
 struct ListNode* head;
 head=(struct ListNode*)malloc(sizeof(struct ListNode));

is this correct?
Since the LeetCode detection mechanism is more strict, we need to assign the pointer field when creating a node.
 
The correct way to create a node:

 struct ListNode {
    
    
     int val;
     struct ListNode *next;
 };
struct ListNode* head;
 head=(struct ListNode*)malloc(sizeof(struct ListNode));
 head->next=NULL;

Summarize

  • Problem: When variables are created, they are not initialized.
  • Solution: After creating the variable, immediately set it blank or assign an initial value.

 
The blogger said one more thing, the above error report only appeared on LeetCode, not on Niuke.com.
Since the testing mechanisms of the two platforms are different, no one is better or worse on this issue.
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/Zhenyu_Coder/article/details/132273442