gpt4 book ai didi

c - 地址 sanitizer : SEGV on unknown address

转载 作者:行者123 更新时间:2023-11-30 16:33:23 25 4
gpt4 key购买 nike

我收到错误

SEGV on unknown address

运行我的程序时。我很确定它来自 fgets() 但我不太清楚为什么。在此之前,我使用 scanf() 并且运行良好。输入很好地进入了数组,但我必须使用 fgets() 来让程序检测新行的输入。谁能帮助我理解为什么会出现此错误并提出解决此问题的方法?

#include <stdio.h>
#include <stdlib.h>

int main(void) {

int x = -1;
int y = -1;

scanf("%d %d", &x, &y);

if (x <= 0 || y <= 0) {
printf("Cannot decode\n");
return 1;
}

char *temp = 0;
int v[x][y];

for (int t=0; t < y; t++) {
for(int i=0; i < x; i++) {

if (fgets(temp, x, stdin)) {
v[i][t] = atoi(temp);
}
}
}

// To test array
int c, o;
for(o=0; o < x; o++) {
for(c=0; c < y; c++) {
printf("%d", v[c][o]);

}
printf("\n");
}

return 0;
}

错误代码:

ASAN:DEADLYSIGNAL
=================================================================
==19==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7fcaaf49d4ab bp 0x
000000000001 sp 0x7ffc84b842b8 T0)
==19==The signal is caused by a WRITE memory access.
==19==Hint: address points to the zero page.
#0 0x7fcaaf49d4aa in __memmove_avx_unaligned_erms (/usr/lib/libc.so.6+0x1574aa)
#1 0x7fcaaf3b4708 in __GI__IO_getline_info (/usr/lib/libc.so.6+0x6e708)
#2 0x7fcaaf3b353c in fgets (/usr/lib/libc.so.6+0x6d53c)
#3 0x5001f4 in main /home/nonogram.c:39:11
#4 0x7fcaaf366f49 in __libc_start_main (/usr/lib/libc.so.6+0x20f49)
#5 0x4187d9 in _start (/home/nonogram+0x4187d9)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/usr/lib/libc.so.6+0x1574aa) in __memmove_avx_unaligned_erms
==19==ABORTING

最佳答案

您必须分配内存来存储您正在读取的信息。使用 callocmalloctemp 指针保留内存。

char *temp = calloc(length_you_expect, sizeof(char))

而不是:

char *temp = 0

使用后记得释放:

免费(临时)

关于c - 地址 sanitizer : SEGV on unknown address,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49794189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com