AddressSanitiser:SEGV在未知地址上 [英] AddressSanitiser: SEGV on unknown address

查看:812
本文介绍了AddressSanitiser:SEGV在未知地址上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到错误

SEGV在未知地址上

SEGV on unknown address

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

while running my program. I am pretty sure it is coming from fgets() but i am not quite sure why. Before this, I was using scanf() and it was working fine. the input was going into the array just fine, but I had to use fgets() for the program to detect an input of a new line. Can anyone help me understand why I am getting this error and suggest a way to fix this problem?

#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

推荐答案

您必须分配内存来存储您正在阅读的信息.用callocmalloc保留temp指针的内存.

You have to allocate memory to store the information you are reading. Reserve memory with calloc or malloc for temp pointer.

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

代替:

char *temp = 0

记住使用后要释放:

free(temp)

这篇关于AddressSanitiser:SEGV在未知地址上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆