I have an error in C++ but don't know how to fix it...


  • 0
    D

    Hello everyone,
    I was writing my solution based on the idea of segment tree for this problem, but I got an issue that I don't understand why and how to deal with. The error goes:
    Line 58: member access within misaligned address 0x000000000005 for type 'struct SumTree', which requires 8 byte alignment

    I think it's related to the memory allocation, but I couldn't figure out how to solve it, even I used

    memset(left,0,sizeof(left));
    

    in my initialization.

    My code is as follow. Can someone help me with this please? I am eager to know because it also happens when I was implementing class Trie sometimes.

    Thank you!

    struct SumTree {
            int val,start,end;
            SumTree* left;
            SumTree* right;
            SumTree(int a, int b): val(0), start(a), end(b), left(NULL), right(NULL) {}
        };
    
    class NumArray {
    public:
        NumArray(vector<int> nums) {
            size=nums.size();
            buildTree(nums,0,size-1);
        }
        
        void update(int i, int val) {
            update(root,i,val);
        }
        
        int sumRange(int i, int j) {
            i=max(i,0);
            j=min(j,size-1);
            return getSum(i,j,root);
        }
    private:
        int size;
        SumTree* root;
        SumTree* buildTree(vector<int> nums, int a, int b) {
            SumTree* root=new SumTree(a,b);
            if (a==b) {
                root->val=nums[a];
                return root;
            }
            int mid=a+(b-a)/2;
            SumTree* left=buildTree(nums,a,mid);
            SumTree* right=buildTree(nums,mid+1,b);
            root->val=left->val+right->val;
            return root;
        }
        int update(SumTree* root, int index, int k) {
            int diff;
            if (root->start==index && root->end==index) {
                diff=k-root->val;
                root->val=k;
                return diff;
            }
            int mid=root->start+(root->end-root->start)/2;
            if (mid<index) {
                diff=update(root->right,index,k);
            }
            else {
                diff=update(root->left,index,k);
            }
            root->val+=diff;
            return diff;
        }
        int getSum(int a, int b, SumTree* root) {
            int res=0;
            if (root->start==a && root->end==b) return root->val;
            int mid=root->start+(root->end-root->start)/2;
            if (mid<a) return getSum(a,b,root->right);
            if (mid>=b) return getSum(a,b,root->left);
            return getSum(a,mid,root->left)+getSum(mid+1,b,root->right);
        }
    };
    
    /**
     * Your NumArray object will be instantiated and called as such:
     * NumArray obj = new NumArray(nums);
     * obj.update(i,val);
     * int param_2 = obj.sumRange(i,j);
     */
    

Log in to reply
 

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