鱼C论坛

 找回密码
 立即注册
查看: 2135|回复: 20

c 语言 scanf函数的使用

[复制链接]
发表于 2022-3-6 11:30:07 | 显示全部楼层 |阅读模式

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

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

x
#include <stdio.h>

int list[6];

int main(void)
{
    int i;
    int ThisSum = 0;
    int MaxSum = 0;

    for (i = 0; i < 6; i++) {
        scanf("%d", &list[i]);            //求助!!!  怎么在这里编译器一直说我没有使用scanf的返回值?那怎么解决呢?
    }

    for (i = 0; i < 6; i++)
    {
        ThisSum += list[i];
        if (ThisSum > MaxSum)
        {
            MaxSum = ThisSum;
        }
        if (ThisSum < 0)
        {
            ThisSum = 0;
        }
    }
    printf("%d\n", MaxSum);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-6 11:32:03 | 显示全部楼层
我这里没有问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-6 11:37:56 | 显示全部楼层
isdkz 发表于 2022-3-6 11:32
我这里没有问题

嗯嗯,我自己用的dev c++也没问题,但是我在其他名平台上写题目就一直出现这个情况
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-6 11:38:44 | 显示全部楼层
投入就放过 发表于 2022-3-6 11:37
嗯嗯,我自己用的dev c++也没问题,但是我在其他名平台上写题目就一直出现这个情况

什么平台,还有报错信息能放上来吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-6 11:49:12 | 显示全部楼层
isdkz 发表于 2022-3-6 11:38
什么平台,还有报错信息能放上来吗?

Pat    还不会发图片
a.c: In function ‘main’:
a.c:12:9: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &list[i]);
         ^~~~~~~~~~~~~~~~~~~~~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-6 11:56:47 | 显示全部楼层
投入就放过 发表于 2022-3-6 11:49
Pat    还不会发图片
a.c: In function ‘main’:
a.c:12:9: warning: ignoring return valu ...

https://blog.csdn.net/weixin_44312186/article/details/86618110
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-6 12:00:32 | 显示全部楼层
用 if(scanf(xxxxxxxxxxxxxxxx)); 试试

有些编译器对于 scanf 很敏感的,因为不安全(溢出问题),所以会报错。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-6 12:01:20 | 显示全部楼层
#define _CRT_SECURE_NO_WARNINGS // <----- 注意这里 -----
#include <stdio.h>

int list[6];

int main(void)
{
    int i;
    int ThisSum = 0;
    int MaxSum = 0;

    for (i = 0; i < 6; i++) {
        if(scanf("%d", &list[i])); // <----- 注意这里 -----
    }

    for (i = 0; i < 6; i++)
    {
        ThisSum += list[i];
        if (ThisSum > MaxSum)
        {
            MaxSum = ThisSum;
        }
        if (ThisSum < 0)
        {
            ThisSum = 0;
        }
    }
    printf("%d\n", MaxSum);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-6 12:17:17 | 显示全部楼层

这样也有警告
$ cat main.c
#include <stdio.h>

int main(void) {
    int n;
    if(scanf("%d", &n));
    printf("%d\n", n);
    return 0;
}
$ clang -g -Wall -o main main.c
main.c:5:24: warning: if statement has empty body [-Wempty-body]
    if(scanf("%d", &n));
                       ^
main.c:5:24: note: put the semicolon on a separate line to silence this warning
1 warning generated.
$ ./main
1234
1234
$
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-6 12:31:42 | 显示全部楼层


可以知道用的是什么编译器吗?我想找这种超级敏感,报错多多的编译器学习学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-6 12:35:54 | 显示全部楼层
傻眼貓咪 发表于 2022-3-6 12:31

可以知道用的是什么编译器吗?我想找这种超级敏感,报错多多的编译器学习学习

好像是clang
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-6 12:37:20 | 显示全部楼层

你的代码提示换行冒号:
if(scanf("%d", &n))
;
这样可以?因为我的编译器没有办法式出这种报错。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-6 12:38:37 | 显示全部楼层

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

使用道具 举报

发表于 2022-3-6 12:57:55 | 显示全部楼层
不同的编译器有这样的问题,但这不影响正常学习。
scanf确实有返回值。你看起不爽可以 int temp; temp = scanf();
这样的warring知道就行了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-6 13:43:16 | 显示全部楼层
傻眼貓咪 发表于 2022-3-6 12:37
你的代码提示换行冒号:这样可以?因为我的编译器没有办法式出这种报错。

嗯,这样确实可以
$ cat main.c
#include <stdio.h>

int main(void) {
    int n;
    if(scanf("%d", &n))
        ;
    printf("%d\n", n);
    return 0;
}
$ clang -g -Wall -o main main.c
$ ./main
123
123
$
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-6 13:53:06 | 显示全部楼层
傻眼貓咪 发表于 2022-3-6 12:31

可以知道用的是什么编译器吗?我想找这种超级敏感,报错多多的编译器学习学习

clang 和 gcc 都可以有
这两个对C/C++ 中的未定义行为的处理,有好多都不一样
例如 i++ + i++,函数参数求值顺序这些
$ cat main.c
#include <stdio.h>

int main(void) {
    int i = 0;
    printf("%d %d %d %d\n", i++, i++, i++, i++);
    printf("%d %d %d %d\n", ++i, ++i, ++i, ++i);
    i = 0;
    printf("%d\n", i++ + i++ + i++);
    printf("%d\n", ++i + ++i + ++i);
    i = 0;
    i = i++;
    printf("%d\n", i);
    i = 0;
    i = ++i;
    printf("%d\n", i);
    i = 0;
    printf("%d\n", i++ + ++i);
    return 0;
}
$ gcc -g -Wall -o main main.c
main.c: In function ‘main’:
main.c:5:45: warning: operation on ‘i’ may be undefined [-Wsequence-point]
    5 |     printf("%d %d %d %d\n", i++, i++, i++, i++);
      |                                            ~^~
main.c:5:45: warning: operation on ‘i’ may be undefined [-Wsequence-point]
main.c:5:45: warning: operation on ‘i’ may be undefined [-Wsequence-point]
main.c:6:44: warning: operation on ‘i’ may be undefined [-Wsequence-point]
    6 |     printf("%d %d %d %d\n", ++i, ++i, ++i, ++i);
      |                                            ^~~
main.c:6:44: warning: operation on ‘i’ may be undefined [-Wsequence-point]
main.c:6:44: warning: operation on ‘i’ may be undefined [-Wsequence-point]
main.c:8:27: warning: operation on ‘i’ may be undefined [-Wsequence-point]
    8 |     printf("%d\n", i++ + i++ + i++);
      |                          ~^~
main.c:8:27: warning: operation on ‘i’ may be undefined [-Wsequence-point]
main.c:9:26: warning: operation on ‘i’ may be undefined [-Wsequence-point]
    9 |     printf("%d\n", ++i + ++i + ++i);
      |                          ^~~
main.c:9:26: warning: operation on ‘i’ may be undefined [-Wsequence-point]
main.c:11:7: warning: operation on ‘i’ may be undefined [-Wsequence-point]
   11 |     i = i++;
      |     ~~^~~~~
main.c:14:7: warning: operation on ‘i’ may be undefined [-Wsequence-point]
   14 |     i = ++i;
      |     ~~^~~~~
main.c:17:21: warning: operation on ‘i’ may be undefined [-Wsequence-point]
   17 |     printf("%d\n", i++ + ++i);
      |                    ~^~
$ ./main
3 2 1 0
8 8 8 8
3
16
0
1
2
$ clang -g -Wall -o main main.c
main.c:5:30: warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
    printf("%d %d %d %d\n", i++, i++, i++, i++);
                             ^    ~~
main.c:6:29: warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
    printf("%d %d %d %d\n", ++i, ++i, ++i, ++i);
                            ^    ~~
main.c:8:21: warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
    printf("%d\n", i++ + i++ + i++);
                    ^     ~~
main.c:9:20: warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
    printf("%d\n", ++i + ++i + ++i);
                   ^     ~~
main.c:11:10: warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
    i = i++;
      ~  ^
main.c:14:9: warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
    i = ++i;
      ~ ^
main.c:17:21: warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
    printf("%d\n", i++ + ++i);
                    ^    ~~
7 warnings generated.
$ ./main
0 1 2 3
5 6 7 8
3
15
0
1
2
$
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-6 14:00:45 | 显示全部楼层
人造人 发表于 2022-3-6 13:53
clang 和 gcc 都可以有
这两个对C/C++ 中的未定义行为的处理,有好多都不一样
例如 i++ + i++,函数参 ...


好的,现在安装 Clang 中 ....,想试试 。我用 gcc 因为同时支持 java 编译,方便。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-6 14:29:39 | 显示全部楼层
傻眼貓咪 发表于 2022-3-6 12:31

可以知道用的是什么编译器吗?我想找这种超级敏感,报错多多的编译器学习学习

其实gcc就自带了内存检查的功能(运行时检查),只是默认没有开启
当然,要你的gcc是新版本的,旧版本的gcc有可能用不了
我不知道gcc从哪一个版本开始添加的内存检查功能,也不关心
反正我的gcc是新版本的,可以用内存检查,^_^
$ gcc --version
gcc (GCC) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$

谈不上最新,但是够用
我没有在windows中尝试过gcc的内存检查功能,可能也能用?不知道

$ cat main.c
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *p = malloc(sizeof(*p));
    *p = 123;
    printf("%d\n", *p);
    //free(p);
    return 0;
}
$ gcc -g -Wall -fsanitize=undefined -fsanitize=leak -fsanitize=address -fno-omit-frame-pointer -o main main.c
$ ./main
123

=================================================================
==987243==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 4 byte(s) in 1 object(s) allocated from:
    #0 0x7ff146f8cdd9 in __interceptor_malloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x560d4f9611ba in main /tmp/C/main.c:5
    #2 0x7ff14638330f in __libc_start_call_main (/usr/lib/libc.so.6+0x2d30f)

SUMMARY: AddressSanitizer: 4 byte(s) leaked in 1 allocation(s).
$ vim main.c
$ cat main.c
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *p = malloc(sizeof(*p));
    *p = 123;
    printf("%d\n", *p);
    free(p);
    return 0;
}
$ gcc -g -Wall -fsanitize=undefined -fsanitize=leak -fsanitize=address -fno-omit-frame-pointer -o main main.c
$ ./main
123
$


$ cat main.c
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int array[10] = {0};
    int n;
    scanf("%d", &n);
    printf("%d\n", array[n]);
    return 0;
}
$ gcc -g -Wall -fsanitize=undefined -fsanitize=leak -fsanitize=address -fno-omit-frame-pointer -o main main.c
$ ./main
10
main.c:8:25: runtime error: index 10 out of bounds for type 'int [10]'
main.c:8:5: runtime error: load of address 0x7ffdd3b78528 with insufficient space for an object of type 'int'
0x7ffdd3b78528: note: pointer points here
 00 00 00 00  00 a6 3c 78 98 8a 28 12  c0 84 b7 d3 fd 7f 00 00  c0 7a fa 5c bf 7f 00 00  41 00 00 00
              ^
=================================================================
==988335==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffdd3b78528 at pc 0x55c36c0db465 bp 0x7ffdd3b784c0 sp 0x7ffdd3b784b0
READ of size 4 at 0x7ffdd3b78528 thread T0
    #0 0x55c36c0db464 in main /tmp/C/main.c:8
    #1 0x7fbf5ba2030f in __libc_start_call_main (/usr/lib/libc.so.6+0x2d30f)
    #2 0x7fbf5ba203c0 in __libc_start_main@GLIBC_2.2.5 (/usr/lib/libc.so.6+0x2d3c0)
    #3 0x55c36c0db124 in _start (/tmp/C/main+0x1124)

Address 0x7ffdd3b78528 is located in stack of thread T0 at offset 88 in frame
    #0 0x55c36c0db208 in main /tmp/C/main.c:4

  This frame has 2 object(s):
    [32, 36) 'n' (line 6)
    [48, 88) 'array' (line 5) <== Memory access at offset 88 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow /tmp/C/main.c:8 in main
Shadow bytes around the buggy address:
  0x10003a767050: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10003a767060: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10003a767070: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10003a767080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10003a767090: 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1 04 f2
=>0x10003a7670a0: 00 00 00 00 00[f3]f3 f3 f3 f3 00 00 00 00 00 00
  0x10003a7670b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10003a7670c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10003a7670d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10003a7670e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10003a7670f0: 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
  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
  Shadow gap:              cc
==988335==ABORTING
$
$
$
$
$
$
$ vim main.c
$ cat main.c
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int array[10] = {0};
    int n;
    scanf("%d", &n);
    array[n] = 123;
    //printf("%d\n", array[n]);
    return 0;
}
$ gcc -g -Wall -fsanitize=undefined -fsanitize=leak -fsanitize=address -fno-omit-frame-pointer -o main main.c
main.c: In function ‘main’:
main.c:5:9: warning: variable ‘array’ set but not used [-Wunused-but-set-variable]
    5 |     int array[10] = {0};
      |         ^~~~~
$ ./main
10
main.c:8:10: runtime error: index 10 out of bounds for type 'int [10]'
main.c:8:14: runtime error: store to address 0x7ffec538b1b8 with insufficient space for an object of type 'int'
0x7ffec538b1b8: note: pointer points here
 00 00 00 00  00 66 e4 ec 51 d5 04 37  50 b1 38 c5 fe 7f 00 00  c0 4a 7a 7c eb 7f 00 00  41 00 00 00
              ^
=================================================================
==988793==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffec538b1b8 at pc 0x55d75b226465 bp 0x7ffec538b150 sp 0x7ffec538b140
WRITE of size 4 at 0x7ffec538b1b8 thread T0
    #0 0x55d75b226464 in main /tmp/C/main.c:8
    #1 0x7feb7b21d30f in __libc_start_call_main (/usr/lib/libc.so.6+0x2d30f)
    #2 0x7feb7b21d3c0 in __libc_start_main@GLIBC_2.2.5 (/usr/lib/libc.so.6+0x2d3c0)
    #3 0x55d75b226124 in _start (/tmp/C/main+0x1124)

Address 0x7ffec538b1b8 is located in stack of thread T0 at offset 88 in frame
    #0 0x55d75b226208 in main /tmp/C/main.c:4

  This frame has 2 object(s):
    [32, 36) 'n' (line 6)
    [48, 88) 'array' (line 5) <== Memory access at offset 88 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow /tmp/C/main.c:8 in main
Shadow bytes around the buggy address:
  0x100058a695e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100058a695f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100058a69600: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100058a69610: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100058a69620: 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1
=>0x100058a69630: 04 f2 00 00 00 00 00[f3]f3 f3 f3 f3 00 00 00 00
  0x100058a69640: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100058a69650: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100058a69660: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100058a69670: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100058a69680: 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
  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
  Shadow gap:              cc
==988793==ABORTING
$

你可能会发现,我有时候用的编译命令是这样的
gcc-debug -o main main.c

这个 gcc-debug 其实是一个 alias
alias gcc-debug='gcc -g -Wall -fsanitize=undefined -fsanitize=leak -fsanitize=address -fno-omit-frame-pointer'

其实下面这两个是一样的
gcc-debug -o main main.c
$ gcc -g -Wall -fsanitize=undefined -fsanitize=leak -fsanitize=address -fno-omit-frame-pointer -o main main.c

我为了避免每一次编译代码都输入那么一长串,就弄了一个alias

这个报错的内容可以说是很详细了,代码的哪一行,变量的值,堆栈的当前状态
这些都给你列出来了,对debug非常有帮助
大部分的逻辑问题(逻辑错误),gcc的运行时检查都能检查出来
大部分的语法问题(语法错误),gcc的-Wall选项也全都给你指出来了
所以,写的代码很难出问题吧?
还有,其实我这边的报错是彩色的,看起来更舒服,^_^

1.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-6 14:34:04 | 显示全部楼层
$ alias gcc-debug
alias gcc-debug='gcc -g -Wall -fsanitize=undefined -fsanitize=leak -fsanitize=address -fno-omit-frame-pointer'
$ cat main.c
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int array[10] = {0};
    int n;
    scanf("%d", &n);
    array[n] = 123;
    //printf("%d\n", array[n]);
    return 0;
}
$ ls
main  main.c
$ rm main
$ ls
main.c
$ gcc-debug -o main main.c
main.c: In function ‘main’:
main.c:5:9: warning: variable ‘array’ set but not used [-Wunused-but-set-variable]
    5 |     int array[10] = {0};
      |         ^~~~~
$ ./main
13
main.c:8:10: runtime error: index 13 out of bounds for type 'int [10]'
main.c:8:14: runtime error: store to address 0x7ffcc7b9b984 with insufficient space for an object of type 'int'
0x7ffcc7b9b984: note: pointer points here
  10 b9 b9 c7 fc 7f 00 00  c0 5a 67 e2 00 7f 00 00  41 00 00 00 60 60 00 00  3c e1 27 e1 00 7f 00 00
              ^
=================================================================
==989484==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffcc7b9b984 at pc 0x559bea5db465 bp 0x7ffcc7b9b910 sp 0x7ffcc7b9b900
WRITE of size 4 at 0x7ffcc7b9b984 thread T0
    #0 0x559bea5db464 in main /tmp/C/main.c:8
    #1 0x7f00e10ee30f in __libc_start_call_main (/usr/lib/libc.so.6+0x2d30f)
    #2 0x7f00e10ee3c0 in __libc_start_main@GLIBC_2.2.5 (/usr/lib/libc.so.6+0x2d3c0)
    #3 0x559bea5db124 in _start (/tmp/C/main+0x1124)

Address 0x7ffcc7b9b984 is located in stack of thread T0 at offset 100 in frame
    #0 0x559bea5db208 in main /tmp/C/main.c:4

  This frame has 2 object(s):
    [32, 36) 'n' (line 6)
    [48, 88) 'array' (line 5) <== Memory access at offset 100 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow /tmp/C/main.c:8 in main
Shadow bytes around the buggy address:
  0x100018f6b6e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100018f6b6f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100018f6b700: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100018f6b710: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100018f6b720: 00 00 00 00 f1 f1 f1 f1 04 f2 00 00 00 00 00 f3
=>0x100018f6b730:[f3]f3 f3 f3 00 00 00 00 00 00 00 00 00 00 00 00
  0x100018f6b740: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100018f6b750: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100018f6b760: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100018f6b770: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100018f6b780: 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
  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
  Shadow gap:              cc
==989484==ABORTING
$
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-6 14:46:19 | 显示全部楼层


我的 gcc 报错机制可能如你所说的没有开启吧,每次我都要设断点才能查看我的内存,哈哈
没有关系,我已经下载 Clang 插件了,随时可以更换 Clang 或 gcc。
Clang 测试成功
01.jpg 02.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-18 08:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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