Leetcode问题#56“合并间隔”已于2019年4月更新,现在使用输入/输出参数int ** returnColumnSizes。当单个指针/数组就可以正常工作时,为什么返回的列大小甚至是双指针也是如此(返回的数组的所有长度都应为2,因此* returnSize的值为2s)。此双指针参数导致堆缓冲区溢出,从而导致程序无法运行到完成。

如果返回的数组数组的大小仅为1,并且执行了底部代码中所示的操作,则该数组将顺利通过。一旦增加数组的数量(以及returnColumnSizes的行),我将得到下面粘贴的错误。

returnColumnSizes[0] = (int*)malloc(sizeof(int));
returnColumnSizes[0][0] = 2;

/* Below code causes overflow

  for (int i = 0; i < *returnSize; i++)
  {
    returnColumnSizes[i] = (int*)malloc(sizeof(int));
    returnColumnSizes[i][0] = 2;
  }

*/

=================================================================
==30==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000000b8 at pc 0x000000405644 bp 0x7ffc16dbf5c0 sp 0x7ffc16dbf5b8
READ of size 4 at 0x6020000000b8 thread T0
    #3 0x7f1287f2e2e0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)

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

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 00 00 fa fa 00 fa fa fa 00 fa fa fa 00 fa
=>0x0c047fff8010: fa fa 00 fa fa fa 00[fa]fa fa 04 fa fa fa fd fa
  0x0c047fff8020: fa fa fd fa fa fa fd fd fa fa fd fd fa fa fd fa
  0x0c047fff8030: fa fa fd fd 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
==30==ABORTING

最佳答案

可能有些因素对情况有一定影响:
1.分配问题:您的意思是,每当您尝试访问数组中的其他对象时,它们都是不可访问的。在数组中没有分配多个对象的情况下可能会发生这种情况。
2.编译器问题,您对此无能为力,但几乎不可能发生。

因此,让我们选择第一个选项。

关于c - 关于Leetcode的更新合并间隔问题#56(特别是C语言),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57002803/

10-17 01:26