疑問Leetcode 215.配列内のK番目の最大要素スタックキャッシュアクセスオーバーフロー

3050 ワード

本来の考えでは、トップスタックを使用することですが、スタックキャッシュアクセスオーバーフローを提示し、エラーメッセージ==AddressSanitizer:heap-buffer-overflow on address 0 x 60300000 028 at pc 0 x 00000401996 bp 0 x 7 fff 9 eefa 330 sp 0 x 7 fff 9 eefa 328
=================================================================
==30==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x603000000028 at pc 0x000000401996 bp 0x7fff9eefa330 sp 0x7fff9eefa328
READ of size 4 at 0x603000000028 thread T0
    #3 0x7f7ce16162e0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)

0x603000000028 is located 0 bytes to the right of 24-byte region [0x603000000010,0x603000000028)
allocated by thread T0 here:
    #0 0x7f7ce2aa02b0 in malloc (/usr/local/lib64/libasan.so.5+0xe82b0)
    #3 0x7f7ce16162e0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)

Shadow bytes around the buggy address:
  0x0c067fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c067fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c067fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c067fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c067fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c067fff8000: fa fa 00 00 00[fa]fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8050: 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
==30==ABORTING

コードは次のとおりです.
//        R[low] R[high]       low        

void Sift(int R[],int low,int high)// R[]         ,           1  
{
    int i = low ,j = 2 * i;
    int temp = R[i];
    while(j <= high)
    {
        if(j < high && R[j] < R[j + 1])
            ++j;
        if(temp < R[j])
        {
            R[i] = R[j];
            i = j;
            j = 2 * i;
        }
        else 
            break;
    }
    R[i] = temp;
}

//     
int findKthLargest(int R[], int n, int k){
    int i ;
    int temp;
    for(int i = n / 2 ; i >= 1;--i)//     
        Sift(R,i,n);
    for(i = n;i >= 2;--i)
    {
        //  3            ,        
        temp = R[1];
        R[1] = R[i];
        R[i] = temp;
        Sift(R,1,i-1);
    }
    
    return(R[k]);
}