鱼C论坛

 找回密码
 立即注册
查看: 1443|回复: 11

[已解决]不好意思,再发个帖子

[复制链接]
发表于 2021-11-24 10:26:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<stdio.h>
  2. int main()
  3. {
  4.         int n,i,j,t;
  5.         //scanf("%d",&n);
  6.         int a[5];
  7.         for(i=0;i<5;i++)scanf("%d",&a[i]);
  8.         for(i=0;i<5;i++){
  9.                 for(j=0;j<5-i;j++)
  10.                         if(a[j]>a[j+1]){
  11.                                 t=a[j];
  12.                                 a[j]=a[j+1];
  13.                                 a[j+1]=t;
  14.                         }
  15.         }
  16.         for(i=0;i<5;i++)printf("%d %d ",i,a[i]);
  17.         return 0;
  18. }
复制代码

那为什么这个代码不越界呢?
最佳答案
2021-11-24 10:48:38
basketmn 发表于 2021-11-24 10:44
当j=4,那a[j+1]=a[5],那就是越界,为什么结果正确,
为什么换成变量n,结果就越界呢?

数组越界访问,结果就一定不是正确的吗?
  1. 数组越界访问会发生什么 是不确定的,我这边是直接报错退出
  2. 讨论数组越界会发生什么 这没有意义,不同的环境很有可能是不一样的
复制代码


这个不确定,也包括结果正确
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-11-24 10:27:17 | 显示全部楼层
这个的结果就是正确的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-24 10:32:42 | 显示全部楼层
输入这个试试

  1. 4
  2. 4 3 2 1
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-24 10:34:54 | 显示全部楼层
输入这个
  1. 5
  2. 5 4 3 2 1
复制代码

  1. $ ./main
  2. 5
  3. 5 4 3 2 1
  4. main.c:11:24: runtime error: index 5 out of bounds for type 'int [5]'
  5. main.c:11:24: runtime error: load of address 0x7ffe517bf1f4 with insufficient space for an object of type 'int'
  6. 0x7ffe517bf1f4: note: pointer points here
  7.   05 00 00 00 60 60 00 00  00 00 00 00 00 00 00 00  01 00 00 00 00 00 00 00  68 f3 7b 51 fe 7f 00 00
  8.               ^
  9. =================================================================
  10. ==114142==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffe517bf1f4 at pc 0x55cf15f1e5c7 bp 0x7ffe517bf190 sp 0x7ffe517bf180
  11. READ of size 4 at 0x7ffe517bf1f4 thread T0
  12.     #0 0x55cf15f1e5c6 in main /tmp/main.c:11
  13.     #1 0x7fbddd563b24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)
  14.     #2 0x55cf15f1e14d in _start (/tmp/main+0x214d)

  15. Address 0x7ffe517bf1f4 is located in stack of thread T0 at offset 52 in frame
  16.     #0 0x55cf15f1e228 in main /tmp/main.c:3

  17.   This frame has 1 object(s):
  18.     [32, 52) 'a' (line 6) <== Memory access at offset 52 overflows this variable
  19. HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
  20.       (longjmp and C++ exceptions *are* supported)
  21. SUMMARY: AddressSanitizer: stack-buffer-overflow /tmp/main.c:11 in main
  22. Shadow bytes around the buggy address:
  23.   0x10004a2efde0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  24.   0x10004a2efdf0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  25.   0x10004a2efe00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  26.   0x10004a2efe10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  27.   0x10004a2efe20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  28. =>0x10004a2efe30: 00 00 00 00 00 00 00 00 f1 f1 f1 f1 00 00[04]f3
  29.   0x10004a2efe40: f3 f3 f3 f3 00 00 00 00 00 00 00 00 00 00 00 00
  30.   0x10004a2efe50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  31.   0x10004a2efe60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  32.   0x10004a2efe70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  33.   0x10004a2efe80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  34. Shadow byte legend (one shadow byte represents 8 application bytes):
  35.   Addressable:           00
  36.   Partially addressable: 01 02 03 04 05 06 07
  37.   Heap left redzone:       fa
  38.   Freed heap region:       fd
  39.   Stack left redzone:      f1
  40.   Stack mid redzone:       f2
  41.   Stack right redzone:     f3
  42.   Stack after return:      f5
  43.   Stack use after scope:   f8
  44.   Global redzone:          f9
  45.   Global init order:       f6
  46.   Poisoned by user:        f7
  47.   Container overflow:      fc
  48.   Array cookie:            ac
  49.   Intra object redzone:    bb
  50.   ASan internal:           fe
  51.   Left alloca redzone:     ca
  52.   Right alloca redzone:    cb
  53.   Shadow gap:              cc
  54. ==114142==ABORTING
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-24 10:39:43 | 显示全部楼层

大佬,我直接输入
5 4 3 2 1
结果就是对的,但是按你那样输入
5
5 4 3 2 1
那结果就是越界,少掉1
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-24 10:43:44 | 显示全部楼层
basketmn 发表于 2021-11-24 10:39
大佬,我直接输入
5 4 3 2 1
结果就是对的,但是按你那样输入

数组越界访问会发生什么 是不确定的,我这边是直接报错退出
讨论数组越界会发生什么 这没有意义,不同的环境很有可能是不一样的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-24 10:44:51 | 显示全部楼层

当j=4,那a[j+1]=a[5],那就是越界,为什么结果正确,
为什么换成变量n,结果就越界呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-24 10:45:53 | 显示全部楼层
basketmn 发表于 2021-11-24 10:39
大佬,我直接输入
5 4 3 2 1
结果就是对的,但是按你那样输入

输入这个同样是报错退出

  1. 5 4 3 2 1
复制代码

  1. $ ./main
  2. 5 4 3 2 1
  3. main.c:11:24: runtime error: index 5 out of bounds for type 'int [5]'
  4. main.c:11:24: runtime error: load of address 0x7ffc7a7cead4 with insufficient space for an object of type 'int'
  5. 0x7ffc7a7cead4: note: pointer points here
  6.   05 00 00 00 60 60 00 00  00 00 00 00 00 00 00 00  01 00 00 00 00 00 00 00  48 ec 7c 7a fc 7f 00 00
  7.               ^
  8. =================================================================
  9. ==114799==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffc7a7cead4 at pc 0x5641cc2235c7 bp 0x7ffc7a7cea70 sp 0x7ffc7a7cea60
  10. READ of size 4 at 0x7ffc7a7cead4 thread T0
  11.     #0 0x5641cc2235c6 in main /tmp/main.c:11
  12.     #1 0x7f958250bb24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)
  13.     #2 0x5641cc22314d in _start (/tmp/main+0x214d)

  14. Address 0x7ffc7a7cead4 is located in stack of thread T0 at offset 52 in frame
  15.     #0 0x5641cc223228 in main /tmp/main.c:3

  16.   This frame has 1 object(s):
  17.     [32, 52) 'a' (line 6) <== Memory access at offset 52 overflows this variable
  18. HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
  19.       (longjmp and C++ exceptions *are* supported)
  20. SUMMARY: AddressSanitizer: stack-buffer-overflow /tmp/main.c:11 in main
  21. Shadow bytes around the buggy address:
  22.   0x10000f4f1d00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  23.   0x10000f4f1d10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  24.   0x10000f4f1d20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  25.   0x10000f4f1d30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  26.   0x10000f4f1d40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  27. =>0x10000f4f1d50: 00 00 00 00 f1 f1 f1 f1 00 00[04]f3 f3 f3 f3 f3
  28.   0x10000f4f1d60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  29.   0x10000f4f1d70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  30.   0x10000f4f1d80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  31.   0x10000f4f1d90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  32.   0x10000f4f1da0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  33. Shadow byte legend (one shadow byte represents 8 application bytes):
  34.   Addressable:           00
  35.   Partially addressable: 01 02 03 04 05 06 07
  36.   Heap left redzone:       fa
  37.   Freed heap region:       fd
  38.   Stack left redzone:      f1
  39.   Stack mid redzone:       f2
  40.   Stack right redzone:     f3
  41.   Stack after return:      f5
  42.   Stack use after scope:   f8
  43.   Global redzone:          f9
  44.   Global init order:       f6
  45.   Poisoned by user:        f7
  46.   Container overflow:      fc
  47.   Array cookie:            ac
  48.   Intra object redzone:    bb
  49.   ASan internal:           fe
  50.   Left alloca redzone:     ca
  51.   Right alloca redzone:    cb
  52.   Shadow gap:              cc
  53. ==114799==ABORTING
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-24 10:46:24 | 显示全部楼层
        试试这个代码呢,这个代码重写了排序部分
  1. #include<stdio.h>
  2. int main(void)
  3. {
  4.         int i , j , n , t                              ;
  5.         scanf("%d" , & n)                              ;
  6.         int a[n]                                       ;
  7.         for(i = 0 ; i < n ; i ++) scanf("%d" , & a[i]) ;
  8.         for(i = 0 ; i < n - 1 ; i ++) {
  9.                 for(j = i + 1 ; j < n ; j ++) {
  10.                         if(a[i] > a[j]) {
  11.                                 t = a[i]               ;
  12.                                 a[i] = a[j]            ;
  13.                                 a[j] = t               ;
  14.                         }
  15.                 }
  16.         }
  17.         printf("%d" , a[0])                            ;
  18.         for(i = 1 ; i < n ; i ++) printf(" %d" , a[i]) ;
  19.         printf("\n")                                   ;
  20. }
复制代码

        编译、运行实况:
  1. D:\0002.Exercise\C>g++ -o x x.c

  2. D:\0002.Exercise\C>x
  3. 5
  4. 3 8 1 4 2
  5. 1 2 3 4 8

  6. D:\0002.Exercise\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-24 10:46:37 | 显示全部楼层
basketmn 发表于 2021-11-24 10:44
当j=4,那a[j+1]=a[5],那就是越界,为什么结果正确,
为什么换成变量n,结果就越界呢?
  1. 数组越界访问会发生什么 是不确定的,我这边是直接报错退出
  2. 讨论数组越界会发生什么 这没有意义,不同的环境很有可能是不一样的
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-24 10:48:38 | 显示全部楼层    本楼为最佳答案   
basketmn 发表于 2021-11-24 10:44
当j=4,那a[j+1]=a[5],那就是越界,为什么结果正确,
为什么换成变量n,结果就越界呢?

数组越界访问,结果就一定不是正确的吗?
  1. 数组越界访问会发生什么 是不确定的,我这边是直接报错退出
  2. 讨论数组越界会发生什么 这没有意义,不同的环境很有可能是不一样的
复制代码


这个不确定,也包括结果正确
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-24 11:03:39 | 显示全部楼层
我明白了,再次感谢人造人和jackz007两位大佬。
谢谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 17:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表