Really Strange Error for C++: Member access within misaligned address

  • 1


    I got this really strange runtime error from Leetcode. But my code runs and outputs the correct result in my local environment. Hoping someone can shed some light why it happens.

    Line 33: member access within misaligned address 0x000000ad0fa7 for type 'struct 
    ListNode', which requires 8 byte alignment 

    I am suspecting the reason why it works in my local but not Leetcode could be due to compiler's differences.

    My local compiler info:

    {0} *****i [09:25:12]  /Library/Developer/CommandLineTools/usr/bin/c++ --version
    Apple LLVM version 8.0.0 (clang-800.0.42.1)
    Target: x86_64-apple-darwin16.1.0
    Thread model: posix
    InstalledDir: /Library/Developer/CommandLineTools/usr/bin

    My source code for this problem:

    class Solution {
        ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
            /* Merge l2 into l1. Return head of l1
            / l2 on the caller's end has to be unchanged.
            // either list is empty
            if (!l1) {return l2;}
            if (!l2) {return l1;}
            ListNode **ppCurNodeInL1 = &l1;
            while(l2) {
    //            ListNode toInsert = *l2;
    //            ListNode *pToInsert = &toInsert;
                ListNode *pToInsert = new ListNode(l2->val);
    //            ListNode toInsert(*l2);
                ListNode *tmp = insert(ppCurNodeInL1, pToInsert);
                ppCurNodeInL1 = &tmp;
                l2 = l2->next;
            return l1;
        ListNode *insert(ListNode **headPtr, ListNode *el) {
            /* insert el at the appropriate location in list beginning at *headPtr and return the node el*/
            // Walk the list to appropriate location for insertion.
            ListNode *head = *headPtr;
            ListNode *cur = head;
            ListNode *prev = 0;
            while(cur && cur->val <= el->val) {
                prev = cur;
                cur = cur->next;
            if (cur == head) {
                // insert at the head
                el->next = head;
                *headPtr = el;
                el = *headPtr;
            } else if (cur == 0) {
                // insert at the end
                prev->next = el;
                el->next = 0;
            } else {
                prev->next = el;
                el->next = cur;
            return el;

    Thanks !

  • 0

    @frankc201106 said in Really Strange Error for C++: Member access within misaligned address:

    ead = *headPtr;
    ListNode *cur = head;
    ListNode *prev = 0;
    while(cur && cur->val <= el

    Did you figure out the problem? I am also facing the same problem, in a similar code block.

  • 0

    Figured out. My structure was uninitialized.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.