鱼C论坛

 找回密码
 立即注册
查看: 1712|回复: 9

[已解决]strncat差一错误

[复制链接]
发表于 2022-7-12 18:49:40 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 liaojuelong 于 2022-7-12 18:51 编辑

课后作业这里没理解,我上机试了一下,以%s输出str,不管有没有减1,似乎对结果影响不大,减1输出“I love Fi”,不减1输出“I love Fis”,那么差一错误怎么体现呢?求解答

题目如下:
请指出下边代码中存在的问题?
#include <stdio.h>
#include <string.h>
int main()
{
char str[10];
strncat(str, "I love FishC.com!", sizeof(str));
return 0;
}
答:不当使用 C 标准库中的 strncat 函数常常会导致差一错误(差一错误是指在计数时由于边界条件判断失误导致结果多了一或少了一的错误)和安全问题。
程序员经常认为 strncat 函数在写入字符串结束符时不会超过最大长度。事实上 strncat 函数会在指定的最大长度之后一字节的位置写入字符串结束符。
上边代码应该改为:strncat(str, "I love FishC.com!", sizeof(str)-1);
最佳答案
2022-7-12 19:26:20
C语言程序中的错误,并不是一定能在第一时间发现
很多错误直到程序运行结束也无法被发现
但是错误就是错误,这个错误很有可能会出现在你无法预期的位置,所以不要存在侥幸心理,说什么有错也没事,反正程序输出结果正常,你如何保证这个错误不会影响最终的输出结果?你无法保证
你可以借助一些调试工具把这个错误的发现时间提前很多

  1. $ cat main.c
  2. #include <stdio.h>
  3. #include <string.h>

  4. int main(void) {
  5.     char buff[5] = {[0] = '\0'};
  6.     strncat(buff, "12345678", sizeof(buff) - 1);
  7.     puts(buff);
  8.     return 0;
  9. }
  10. $ gcc-debug -o main main.c
  11. $ ./main
  12. 1234
  13. $ vim main.c
  14. $ cat main.c
  15. #include <stdio.h>
  16. #include <string.h>

  17. int main(void) {
  18.     char buff[5] = {[0] = '\0'};
  19.     strncat(buff, "12345678", sizeof(buff));
  20.     puts(buff);
  21.     return 0;
  22. }
  23. $ gcc-debug -o main main.c
  24. main.c: In function ‘main’:
  25. main.c:6:5: warning: ‘strncat’ specified bound 5 equals destination size [-Wstringop-overflow=]
  26.     6 |     strncat(buff, "12345678", sizeof(buff));
  27.       |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28. $ ./main
  29. =================================================================
  30. ==2151829==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffd3d4f0955 at pc 0x7f6845460665 bp 0x7ffd3d4f0920 sp 0x7ffd3d4f00c8
  31. WRITE of size 6 at 0x7ffd3d4f0955 thread T0
  32.     #0 0x7f6845460664 in __interceptor_strncat /usr/src/debug/gcc/libsanitizer/asan/asan_interceptors.cpp:399
  33.     #1 0x5629182842c9 in main /tmp/main.c:6
  34.     #2 0x7f684482928f  (/usr/lib/libc.so.6+0x2928f)
  35.     #3 0x7f6844829349 in __libc_start_main (/usr/lib/libc.so.6+0x29349)
  36.     #4 0x5629182840e4 in _start (/tmp/main+0x10e4)

  37. Address 0x7ffd3d4f0955 is located in stack of thread T0 at offset 37 in frame
  38.     #0 0x5629182841c8 in main /tmp/main.c:4

  39.   This frame has 1 object(s):
  40.     [32, 37) 'buff' (line 5) <== Memory access at offset 37 overflows this variable
  41. HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
  42.       (longjmp and C++ exceptions *are* supported)
  43. SUMMARY: AddressSanitizer: stack-buffer-overflow /usr/src/debug/gcc/libsanitizer/asan/asan_interceptors.cpp:399 in __interceptor_strncat
  44. Shadow bytes around the buggy address:
  45.   0x100027a960d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  46.   0x100027a960e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  47.   0x100027a960f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  48.   0x100027a96100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  49.   0x100027a96110: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  50. =>0x100027a96120: 00 00 00 00 00 00 f1 f1 f1 f1[05]f3 f3 f3 00 00
  51.   0x100027a96130: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  52.   0x100027a96140: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  53.   0x100027a96150: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  54.   0x100027a96160: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  55.   0x100027a96170: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  56. Shadow byte legend (one shadow byte represents 8 application bytes):
  57.   Addressable:           00
  58.   Partially addressable: 01 02 03 04 05 06 07
  59.   Heap left redzone:       fa
  60.   Freed heap region:       fd
  61.   Stack left redzone:      f1
  62.   Stack mid redzone:       f2
  63.   Stack right redzone:     f3
  64.   Stack after return:      f5
  65.   Stack use after scope:   f8
  66.   Global redzone:          f9
  67.   Global init order:       f6
  68.   Poisoned by user:        f7
  69.   Container overflow:      fc
  70.   Array cookie:            ac
  71.   Intra object redzone:    bb
  72.   ASan internal:           fe
  73.   Left alloca redzone:     ca
  74.   Right alloca redzone:    cb
  75. ==2151829==ABORTING
  76. $
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-7-12 19:26:20 | 显示全部楼层    本楼为最佳答案   
C语言程序中的错误,并不是一定能在第一时间发现
很多错误直到程序运行结束也无法被发现
但是错误就是错误,这个错误很有可能会出现在你无法预期的位置,所以不要存在侥幸心理,说什么有错也没事,反正程序输出结果正常,你如何保证这个错误不会影响最终的输出结果?你无法保证
你可以借助一些调试工具把这个错误的发现时间提前很多

  1. $ cat main.c
  2. #include <stdio.h>
  3. #include <string.h>

  4. int main(void) {
  5.     char buff[5] = {[0] = '\0'};
  6.     strncat(buff, "12345678", sizeof(buff) - 1);
  7.     puts(buff);
  8.     return 0;
  9. }
  10. $ gcc-debug -o main main.c
  11. $ ./main
  12. 1234
  13. $ vim main.c
  14. $ cat main.c
  15. #include <stdio.h>
  16. #include <string.h>

  17. int main(void) {
  18.     char buff[5] = {[0] = '\0'};
  19.     strncat(buff, "12345678", sizeof(buff));
  20.     puts(buff);
  21.     return 0;
  22. }
  23. $ gcc-debug -o main main.c
  24. main.c: In function ‘main’:
  25. main.c:6:5: warning: ‘strncat’ specified bound 5 equals destination size [-Wstringop-overflow=]
  26.     6 |     strncat(buff, "12345678", sizeof(buff));
  27.       |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28. $ ./main
  29. =================================================================
  30. ==2151829==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffd3d4f0955 at pc 0x7f6845460665 bp 0x7ffd3d4f0920 sp 0x7ffd3d4f00c8
  31. WRITE of size 6 at 0x7ffd3d4f0955 thread T0
  32.     #0 0x7f6845460664 in __interceptor_strncat /usr/src/debug/gcc/libsanitizer/asan/asan_interceptors.cpp:399
  33.     #1 0x5629182842c9 in main /tmp/main.c:6
  34.     #2 0x7f684482928f  (/usr/lib/libc.so.6+0x2928f)
  35.     #3 0x7f6844829349 in __libc_start_main (/usr/lib/libc.so.6+0x29349)
  36.     #4 0x5629182840e4 in _start (/tmp/main+0x10e4)

  37. Address 0x7ffd3d4f0955 is located in stack of thread T0 at offset 37 in frame
  38.     #0 0x5629182841c8 in main /tmp/main.c:4

  39.   This frame has 1 object(s):
  40.     [32, 37) 'buff' (line 5) <== Memory access at offset 37 overflows this variable
  41. HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
  42.       (longjmp and C++ exceptions *are* supported)
  43. SUMMARY: AddressSanitizer: stack-buffer-overflow /usr/src/debug/gcc/libsanitizer/asan/asan_interceptors.cpp:399 in __interceptor_strncat
  44. Shadow bytes around the buggy address:
  45.   0x100027a960d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  46.   0x100027a960e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  47.   0x100027a960f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  48.   0x100027a96100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  49.   0x100027a96110: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  50. =>0x100027a96120: 00 00 00 00 00 00 f1 f1 f1 f1[05]f3 f3 f3 00 00
  51.   0x100027a96130: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  52.   0x100027a96140: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  53.   0x100027a96150: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  54.   0x100027a96160: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  55.   0x100027a96170: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  56. Shadow byte legend (one shadow byte represents 8 application bytes):
  57.   Addressable:           00
  58.   Partially addressable: 01 02 03 04 05 06 07
  59.   Heap left redzone:       fa
  60.   Freed heap region:       fd
  61.   Stack left redzone:      f1
  62.   Stack mid redzone:       f2
  63.   Stack right redzone:     f3
  64.   Stack after return:      f5
  65.   Stack use after scope:   f8
  66.   Global redzone:          f9
  67.   Global init order:       f6
  68.   Poisoned by user:        f7
  69.   Container overflow:      fc
  70.   Array cookie:            ac
  71.   Intra object redzone:    bb
  72.   ASan internal:           fe
  73.   Left alloca redzone:     ca
  74.   Right alloca redzone:    cb
  75. ==2151829==ABORTING
  76. $
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-12 19:29:13 | 显示全部楼层
本帖最后由 人造人 于 2022-7-12 19:31 编辑

另外,只有这一个问题吗?

  1. $ cat main.c
  2. #include <stdio.h>
  3. #include <string.h>

  4. int main() {
  5.     char str[10];
  6.     strncat(str, "I love FishC.com!", sizeof(str));
  7.     return 0;
  8. }
  9. $ gcc-debug -o main main.c
  10. main.c: In function ‘main’:
  11. main.c:6:5: warning: ‘strncat’ specified bound 10 equals destination size [-Wstringop-overflow=]
  12.     6 |     strncat(str, "I love FishC.com!", sizeof(str));
  13.       |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  14. $ ./main
  15. =================================================================
  16. ==2153892==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffe4639e55a at pc 0x7f56ae060665 bp 0x7ffe4639e520 sp 0x7ffe4639dcc8
  17. WRITE of size 11 at 0x7ffe4639e55a thread T0
  18.     #0 0x7f56ae060664 in __interceptor_strncat /usr/src/debug/gcc/libsanitizer/asan/asan_interceptors.cpp:399
  19.     #1 0x55f82a517234 in main /tmp/main.c:6
  20.     #2 0x7f56ad42928f  (/usr/lib/libc.so.6+0x2928f)
  21.     #3 0x7f56ad429349 in __libc_start_main (/usr/lib/libc.so.6+0x29349)
  22.     #4 0x55f82a5170c4 in _start (/tmp/main+0x10c4)

  23. Address 0x7ffe4639e55a is located in stack of thread T0 at offset 42 in frame
  24.     #0 0x55f82a5171a8 in main /tmp/main.c:4

  25.   This frame has 1 object(s):
  26.     [32, 42) 'str' (line 5) <== Memory access at offset 42 overflows this variable
  27. HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
  28.       (longjmp and C++ exceptions *are* supported)
  29. SUMMARY: AddressSanitizer: stack-buffer-overflow /usr/src/debug/gcc/libsanitizer/asan/asan_interceptors.cpp:399 in __interceptor_strncat
  30. Shadow bytes around the buggy address:
  31.   0x100048c6bc50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  32.   0x100048c6bc60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  33.   0x100048c6bc70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  34.   0x100048c6bc80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  35.   0x100048c6bc90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  36. =>0x100048c6bca0: 00 00 00 00 00 00 f1 f1 f1 f1 00[02]f3 f3 00 00
  37.   0x100048c6bcb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  38.   0x100048c6bcc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  39.   0x100048c6bcd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  40.   0x100048c6bce0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  41.   0x100048c6bcf0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  42. Shadow byte legend (one shadow byte represents 8 application bytes):
  43.   Addressable:           00
  44.   Partially addressable: 01 02 03 04 05 06 07
  45.   Heap left redzone:       fa
  46.   Freed heap region:       fd
  47.   Stack left redzone:      f1
  48.   Stack mid redzone:       f2
  49.   Stack right redzone:     f3
  50.   Stack after return:      f5
  51.   Stack use after scope:   f8
  52.   Global redzone:          f9
  53.   Global init order:       f6
  54.   Poisoned by user:        f7
  55.   Container overflow:      fc
  56.   Array cookie:            ac
  57.   Intra object redzone:    bb
  58.   ASan internal:           fe
  59.   Left alloca redzone:     ca
  60.   Right alloca redzone:    cb
  61. ==2153892==ABORTING
  62. $
  63. $
  64. $
  65. $ vim main.c
  66. $ cat main.c
  67. #include <stdio.h>
  68. #include <string.h>

  69. int main() {
  70.     char str[10];
  71.     strncat(str, "I love FishC.com!", sizeof(str) - 1);
  72.     return 0;
  73. }
  74. $ gcc-debug -o main main.c
  75. $ ./main
  76. =================================================================
  77. ==2154032==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffc00d62f7a at pc 0x7f0c1cc60665 bp 0x7ffc00d62f40 sp 0x7ffc00d626e8
  78. WRITE of size 10 at 0x7ffc00d62f7a thread T0
  79.     #0 0x7f0c1cc60664 in __interceptor_strncat /usr/src/debug/gcc/libsanitizer/asan/asan_interceptors.cpp:399
  80.     #1 0x55758509b234 in main /tmp/main.c:6
  81.     #2 0x7f0c1c02928f  (/usr/lib/libc.so.6+0x2928f)
  82.     #3 0x7f0c1c029349 in __libc_start_main (/usr/lib/libc.so.6+0x29349)
  83.     #4 0x55758509b0c4 in _start (/tmp/main+0x10c4)

  84. Address 0x7ffc00d62f7a is located in stack of thread T0 at offset 42 in frame
  85.     #0 0x55758509b1a8 in main /tmp/main.c:4

  86.   This frame has 1 object(s):
  87.     [32, 42) 'str' (line 5) <== Memory access at offset 42 overflows this variable
  88. HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
  89.       (longjmp and C++ exceptions *are* supported)
  90. SUMMARY: AddressSanitizer: stack-buffer-overflow /usr/src/debug/gcc/libsanitizer/asan/asan_interceptors.cpp:399 in __interceptor_strncat
  91. Shadow bytes around the buggy address:
  92.   0x1000001a4590: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  93.   0x1000001a45a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  94.   0x1000001a45b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  95.   0x1000001a45c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  96.   0x1000001a45d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  97. =>0x1000001a45e0: 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1 00[02]
  98.   0x1000001a45f0: f3 f3 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  99.   0x1000001a4600: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  100.   0x1000001a4610: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  101.   0x1000001a4620: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  102.   0x1000001a4630: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  103. Shadow byte legend (one shadow byte represents 8 application bytes):
  104.   Addressable:           00
  105.   Partially addressable: 01 02 03 04 05 06 07
  106.   Heap left redzone:       fa
  107.   Freed heap region:       fd
  108.   Stack left redzone:      f1
  109.   Stack mid redzone:       f2
  110.   Stack right redzone:     f3
  111.   Stack after return:      f5
  112.   Stack use after scope:   f8
  113.   Global redzone:          f9
  114.   Global init order:       f6
  115.   Poisoned by user:        f7
  116.   Container overflow:      fc
  117.   Array cookie:            ac
  118.   Intra object redzone:    bb
  119.   ASan internal:           fe
  120.   Left alloca redzone:     ca
  121.   Right alloca redzone:    cb
  122. ==2154032==ABORTING
  123. $
  124. $
  125. $
  126. $ vim main.c
  127. $ cat main.c
  128. #include <stdio.h>
  129. #include <string.h>

  130. int main() {
  131.     char str[10];
  132.     str[0] = '\0';      // ****************************
  133.     strncat(str, "I love FishC.com!", sizeof(str) - 1);
  134.     return 0;
  135. }
  136. $ gcc-debug -o main main.c
  137. $ ./main
  138. $
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-7-12 19:58:01 | 显示全部楼层
人造人 发表于 2022-7-12 19:29
另外,只有这一个问题吗?

还有一个问题,比如我定义一个字符串数组char a[5]=“abcdefg”,%s输出是abcde'\0',为什么不是输出abcd'\0',或者abcdefg'\0'呢这也是疑惑的地方
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-12 20:22:31 | 显示全部楼层
liaojuelong 发表于 2022-7-12 19:58
还有一个问题,比如我定义一个字符串数组char a[5]=“abcdefg”,%s输出是abcde'\0',为什么不 ...

又是一个错误的代码,我这边直接报错
  1. $ cat main.c
  2. #include <stdio.h>

  3. int main(void) {
  4.     char a[5] = "abcdefg";
  5.     printf("%s\n", a);
  6.     return 0;
  7. }
  8. $ gcc-debug -o main main.c
  9. main.c: In function ‘main’:
  10. main.c:4:17: warning: initializer-string for array of ‘char’ is too long
  11.     4 |     char a[5] = "abcdefg";
  12.       |                 ^~~~~~~~~
  13. $ ./main
  14. =================================================================
  15. ==2169117==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffd35b6c095 at pc 0x7f328524a1cb bp 0x7ffd35b6c060 sp 0x7ffd35b6b808
  16. READ of size 7 at 0x7ffd35b6c095 thread T0
  17.     #0 0x7f328524a1ca in __interceptor_puts /usr/src/debug/gcc/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:1283
  18.     #1 0x55ede3cdf2a4 in main /tmp/main.c:5
  19.     #2 0x7f328462928f  (/usr/lib/libc.so.6+0x2928f)
  20.     #3 0x7f3284629349 in __libc_start_main (/usr/lib/libc.so.6+0x29349)
  21.     #4 0x55ede3cdf0d4 in _start (/tmp/main+0x10d4)

  22. Address 0x7ffd35b6c095 is located in stack of thread T0 at offset 37 in frame
  23.     #0 0x55ede3cdf1b8 in main /tmp/main.c:3

  24.   This frame has 1 object(s):
  25.     [32, 37) 'a' (line 4) <== Memory access at offset 37 overflows this variable
  26. HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
  27.       (longjmp and C++ exceptions *are* supported)
  28. SUMMARY: AddressSanitizer: stack-buffer-overflow /usr/src/debug/gcc/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:1283 in __interceptor_puts
  29. Shadow bytes around the buggy address:
  30.   0x100026b657c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  31.   0x100026b657d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  32.   0x100026b657e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  33.   0x100026b657f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  34.   0x100026b65800: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1
  35. =>0x100026b65810: f1 f1[05]f3 f3 f3 00 00 00 00 00 00 00 00 00 00
  36.   0x100026b65820: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  37.   0x100026b65830: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  38.   0x100026b65840: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  39.   0x100026b65850: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  40.   0x100026b65860: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  41. Shadow byte legend (one shadow byte represents 8 application bytes):
  42.   Addressable:           00
  43.   Partially addressable: 01 02 03 04 05 06 07
  44.   Heap left redzone:       fa
  45.   Freed heap region:       fd
  46.   Stack left redzone:      f1
  47.   Stack mid redzone:       f2
  48.   Stack right redzone:     f3
  49.   Stack after return:      f5
  50.   Stack use after scope:   f8
  51.   Global redzone:          f9
  52.   Global init order:       f6
  53.   Poisoned by user:        f7
  54.   Container overflow:      fc
  55.   Array cookie:            ac
  56.   Intra object redzone:    bb
  57.   ASan internal:           fe
  58.   Left alloca redzone:     ca
  59.   Right alloca redzone:    cb
  60. ==2169117==ABORTING
  61. $
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-12 20:25:38 | 显示全部楼层
liaojuelong 发表于 2022-7-12 19:58
还有一个问题,比如我定义一个字符串数组char a[5]=“abcdefg”,%s输出是abcde'\0',为什么不 ...
  1. char a[5] = "abcdefg";
  2. 你需要明白,这个数组a有几个元素?是5个?还是7个?还是8个?
  3. 这个数组保存的是哪些字符?
  4. a[0] = 什么?
  5. a[1] 呢?
  6. 还有其他
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-7-12 21:02:27 | 显示全部楼层

我用devc++5.11是跳出warning,但是可以输出。然后刚刚我又下了一个vc++6.0,直接error了。。。难道是我用的编译器不对要用哪个比较好呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-12 21:12:54 | 显示全部楼层
liaojuelong 发表于 2022-7-12 21:02
我用devc++5.11是跳出warning,但是可以输出。然后刚刚我又下了一个vc++6.0,直接error了。。。难道是我 ...

用最新的编译器就好
我用的gcc
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-7-12 22:08:44 | 显示全部楼层
人造人 发表于 2022-7-12 21:12
用最新的编译器就好
我用的gcc

大佬推荐一个比较新的c编译器
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-12 22:14:03 | 显示全部楼层
liaojuelong 发表于 2022-7-12 22:08
大佬推荐一个比较新的c编译器

gcc (GCC) 12.1.0

  1. $ gcc --version
  2. gcc (GCC) 12.1.0
  3. Copyright (C) 2022 Free Software Foundation, Inc.
  4. This is free software; see the source for copying conditions.  There is NO
  5. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  6. $
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-11 00:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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