samedi 3 octobre 2020

Using vectors in LC easy problem keeps leading to some kind of seg fault error

Leetcode Question:

Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn].

Return the array in the form [x1,y1,x2,y2,...,xn,yn].

 

Example 1:

Input: nums = [2,5,1,3,4,7], n = 3
Output: [2,3,5,4,1,7] 
Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].

Here's my code:

class Solution {
public:
    vector<int> shuffle(vector<int>& nums, int n) {
        /*
        1    2  3  4  5  6
        [x1,x2,x3,y1,y2,y3]
        [x1,y1,x2,y2,x3,y3]
        
        when combined,
        x will always be odd, y will always be even
        
        */
        
        //declare vectors
        vector<int> x (n, 0);
        vector<int> y (n, 0);
        vector<int> result(2*n, 0);
        
        //split nums param array
        for(int i = 0; i < n; i++){
            x[i] = nums[i];
        }
        
        for(int i = n; i < sizeof(nums); i++){
            y[i] = nums[i];
        }
        
        int xCount = 0;
        int yCount = 0;
        
        //combine
        for(int i = 0; i < result.size(); i++){
            if(i % 2 != 0){ //if odd
                result[i] = x[xCount];
                xCount++;
            }
            else{
                result[i] = y[yCount];
                yCount++;
            }
        }
        
        return result;      
    }
};

I keep getting this compile error:

=================================================================
==30==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000000dc at pc 0x000000376b62 bp 0x7ffeb6f4acb0 sp 0x7ffeb6f4aca8
WRITE of size 4 at 0x6020000000dc thread T0
    #3 0x7fb40ba770b2  (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
0x6020000000dc is located 0 bytes to the right of 12-byte region [0x6020000000d0,0x6020000000dc)
allocated by thread T0 here:
    #10 0x7fb40ba770b2  (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
Shadow bytes around the buggy address:
  0x0c047fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff8000: fa fa fd fa fa fa fd fa fa fa fd fa fa fa fd fa
=>0x0c047fff8010: fa fa fd fd fa fa 00 04 fa fa 00[04]fa fa fa fa
  0x0c047fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==30==ABORTING

What the heck is going on?

I believe it may have to do with maybe misusing a vector...or accessing an index that isn't there...but I'm going over my code and I believe it's within bounds (though solution may be incorrect). Just scratching my head on this compile error...

Aucun commentaire:

Enregistrer un commentaire