在C中分配一个int数组,但得到一个额外的int 32767

在C中分配一个int数组,但得到一个额外的int 32767,c,arrays,pointers,C,Arrays,Pointers,我有以下C代码: int main(){ int array[] = {1,2,3,3,4,5,6,7,8}; printf("%d\n", array[8]); printf("%d\n", array[9]); } 当我执行它时,我得到的结果是8和32767。我不明白为什么 更新 关于3,3的事我很抱歉。 真正让我困惑的是32767这个数字,如果它是一个未定义的行为,为什么它总是32767?我发现了一件奇怪的事: int main(){ int array

我有以下C代码:

int main(){
    int array[] = {1,2,3,3,4,5,6,7,8};
    printf("%d\n", array[8]);
    printf("%d\n", array[9]);
}
当我执行它时,我得到的结果是8和32767。我不明白为什么


更新

关于3,3的事我很抱歉。 真正让我困惑的是32767这个数字,如果它是一个未定义的行为,为什么它总是32767?我发现了一件奇怪的事:

int main(){
    int arrayB[] = {7,4,3,4,1};
    for(int i = 0; *(arrayB+i); i++){
        printf("%d\n",*(arrayB+i) );
    }
}


我写了这个,号码32767又出现了。令人毛骨悚然的是,如果我将数组更改为任何其他长度(删除或添加数字),效果会很好

您正在获得未定义的行为

不能打印9元素数组的第10个元素,这是无效代码

对于
array[8]
,您只得到
8
,因为在初始值设定项列表中有
3
两次

您的阵列在内存中看起来如下所示:

       +---+---+---+---+---+---+---+---+---+
index: | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
       +---+---+---+---+---+---+---+---+---+
 data: | 1 | 2 | 3 | 3 | 4 | 5 | 6 | 7 | 8 |
       +---+---+---+---+---+---+---+---+---+

因此,您可以看到
数组[8]
中的数据实际上是8,但这是数组中的最后一个元素。如果使用大于
8
的索引进行索引,则会得到未定义的行为。

数组索引从
0
开始。所以您不应该访问第10个元素

array[0] is storing 1.
array[1] is storing 2.
array[2] is storing 3.
array[3] is storing 3.
array[4] is storing 4.
array[5] is storing 5.
array[6] is storing 6.
array[7] is storing 7.
array[8] is storing 8.

当您尝试访问
数组[9]
时,您将获得未定义的行为。

该数组有9个有效位置,但您必须考虑到它们在C中以0开始索引

因此,如果需要第一个值:数组[0]

如果需要最后一个值:数组[8]

数组[9]不是有效位置


希望这会有所帮助。

在C中,未初始化的变量会获取垃圾值,这在您的情况下会发生,因为未定义数组[9],因为C中的索引以0而不是1开始

在C中,数组元素从零到长度减1进行计数。如果您使用GCC编译器,您可以添加选项
-fsanize=address
,以检测这样的一个一个错误

资料来源:

$ cat test.c
#include <stdio.h>

int main(void)
{
        int array[] = {1, 2, 3, 3, 4, 5, 6, 7, 8};

        printf("%d\n", array[8]);
        printf("%d\n", array[9]);
        return 0;
}
输出:

$ ./test
8
=================================================================
==4517== ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fff70a43794 at pc 0x400b2b bp 0x7fff70a43740 sp 0x7fff70a43738
READ of size 4 at 0x7fff70a43794 thread T0
    #0 0x400b2a (/tmp/test+0x400b2a)
    #1 0x7f1ff1180ec4 (/lib/x86_64-linux-gnu/libc-2.19.so+0x21ec4)
    #2 0x400788 (/tmp/test+0x400788)
Address 0x7fff70a43794 is located at offset 68 in frame <main> of T0's stack:
  This frame has 1 object(s):
    [32, 68) 'array'
HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext
      (longjmp and C++ exceptions *are* supported)
Shadow bytes around the buggy address:
  0x10006e1406a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006e1406b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006e1406c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006e1406d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006e1406e0: 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1 00 00
=>0x10006e1406f0: 00 00[04]f4 f4 f4 f3 f3 f3 f3 00 00 00 00 00 00
  0x10006e140700: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006e140710: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006e140720: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006e140730: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006e140740: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
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
  Heap righ redzone:     fb
  Freed Heap region:     fd
  Stack left redzone:    f1
  Stack mid redzone:     f2
  Stack right redzone:   f3
  Stack partial redzone: f4
  Stack after return:    f5
  Stack use after scope: f8
  Global redzone:        f9
  Global init order:     f6
  Poisoned by user:      f7
  ASan internal:         fe
==4517== ABORTING
$/测试
8.
=================================================================
==4517==错误:AddressSanitizer:在pc 0x400b2b bp 0x7fff70a43740 sp 0x7fff70a43738上,地址0x7fff70a43794上的堆栈缓冲区溢出
在0x7FFF70A43994螺纹T0处读取尺寸4
#0 0x400b2a(/tmp/test+0x400b2a)
#1 0x7f1ff1180ec4(/lib/x86_64-linux-gnu/libc-2.19.so+0x21ec4)
#2 0x400788(/tmp/test+0x400788)
地址0x7FFF70A43994位于T0堆栈帧中的偏移量68处:
此帧有1个对象:
[32,68]“阵列”
提示:如果您的程序使用某些自定义堆栈展开机制或swapcontext,则这可能是误报
(LangJMP和C++异常*支持*)
错误地址周围的阴影字节:
0x10006e1406a0:00
0x10006e1406b0:00 00
0x10006e1406c0:00
0x10006e1406d0:00
0x10006e1406e0:00 00 00 f1 00 00
=>0x10006e1406f0:00 00[04]f4 f4 f4 f3 f3 f3 f3 00 00 00
0x10006e140700:00 00
0x10006e140710:00 00
0x10006e140720:00 00
0x10006e140730:00
0x10006e140740:00 00
阴影字节图例(一个阴影字节代表8个应用程序字节):
地址:00
部分可寻址:01 02 03 04 05 06 07
堆左红色区域:fa
堆右侧红色区域:fb
释放的堆区域:fd
堆栈左红色区域:f1
堆栈中间红色区域:f2
堆栈右红色区域:f3
堆栈部分红色区域:f4
返回后堆栈:f5
堆栈在作用域之后使用:f8
全球红色地带:f9
全局初始化顺序:f6
用户中毒:f7
ASan内部:fe
==4517==中止

总是在启用所有警告的情况下编译也是一个好主意。

在C中,数组元素的地址为[0],[1],…[8],这样表示很混乱。数组有9个元素,他试图访问数组[9],但这是不正确的,因为在C中,第一个元素是数组[0],第九个是数组[8].@Carles谢谢,我计算错了,因为有一个令人困惑的
1,2,3,3,[…]
初始值设定项。没问题,我一开始也遇到了澄清,它不仅可以包含任何值,还可以使程序崩溃。@DrewMcGowen:我不知道它也会使程序崩溃!谢谢你提供的信息:)
$ ./test
8
=================================================================
==4517== ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fff70a43794 at pc 0x400b2b bp 0x7fff70a43740 sp 0x7fff70a43738
READ of size 4 at 0x7fff70a43794 thread T0
    #0 0x400b2a (/tmp/test+0x400b2a)
    #1 0x7f1ff1180ec4 (/lib/x86_64-linux-gnu/libc-2.19.so+0x21ec4)
    #2 0x400788 (/tmp/test+0x400788)
Address 0x7fff70a43794 is located at offset 68 in frame <main> of T0's stack:
  This frame has 1 object(s):
    [32, 68) 'array'
HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext
      (longjmp and C++ exceptions *are* supported)
Shadow bytes around the buggy address:
  0x10006e1406a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006e1406b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006e1406c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006e1406d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006e1406e0: 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1 00 00
=>0x10006e1406f0: 00 00[04]f4 f4 f4 f3 f3 f3 f3 00 00 00 00 00 00
  0x10006e140700: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006e140710: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006e140720: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006e140730: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006e140740: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
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
  Heap righ redzone:     fb
  Freed Heap region:     fd
  Stack left redzone:    f1
  Stack mid redzone:     f2
  Stack right redzone:   f3
  Stack partial redzone: f4
  Stack after return:    f5
  Stack use after scope: f8
  Global redzone:        f9
  Global init order:     f6
  Poisoned by user:      f7
  ASan internal:         fe
==4517== ABORTING