大小 8 2 的读/写无效

2024-06-18

在处理我的学校项目时,在 Unix 学校服务器上编译我的项目后,我不断收到来自 Valgrind 的以下错误,并且无法运行该程序,因为我收到“分段错误:11”。

==95183== Memcheck, a memory error detector
==95183== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==95183== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==95183== Command: ./Euler
==95183==
==95183== Invalid read of size 8
==95183==    at 0x400B65: GInit (Euler.c:64)
==95183==    by 0x400DD1: main (Euler.c:118)
==95183==  Address 0x1786100 is 0 bytes after a block of size 48 alloc'd
==95183==    at 0x100688B: malloc (in /usr/local/lib/valgrind/vgpreload_memcheck-amd64-freebsd.so)
==95183==    by 0x400A80: GInit (Euler.c:43)
==95183==    by 0x400DD1: main (Euler.c:118)
==95183==
==95183== Invalid write of size 4
==95183==    at 0x400B6B: GInit (Euler.c:64)
==95183==    by 0x400DD1: main (Euler.c:118)
==95183==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==95183==
==95183==
==95183== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==95183==  Access not within mapped region at address 0x0
==95183==    at 0x400B6B: GInit (Euler.c:64)
==95183==    by 0x400DD1: main (Euler.c:118)
==95183==  If you believe this happened as a result of a stack
==95183==  overflow in your program's main thread (unlikely but
==95183==  possible), you can try to increase the size of the
==95183==  main thread stack using the --main-stacksize= flag.
==95183==  The main thread stack size used in this run was 16777216.
==95183==
==95183== HEAP SUMMARY:
==95183==     in use at exit: 32,981 bytes in 16 blocks
==95183==   total heap usage: 16 allocs, 0 frees, 32,981 bytes allocated
==95183==
==95183== LEAK SUMMARY:
==95183==    definitely lost: 0 bytes in 0 blocks
==95183==    indirectly lost: 0 bytes in 0 blocks
==95183==      possibly lost: 0 bytes in 0 blocks
==95183==    still reachable: 32,981 bytes in 16 blocks
==95183==         suppressed: 0 bytes in 0 blocks
==95183== Reachable blocks (those to which a pointer was found) are not shown.
==95183== To see them, rerun with: --leak-check=full --show-reachable=yes
==95183==
==95183== For counts of detected and suppressed errors, rerun with: -v
==95183== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Segmentation fault: 11
eva ~/Algoritmy/Euler> make
gcc -Wall -std=c99 -pedantic -lm -g -o Euler Euler.c
eva ~/Algoritmy/Euler> ./Euler
Segmentation fault: 11 (core dumped [obraz paměti uložen])
eva ~/Algoritmy/Euler>  valgrind --leak-check=yes ./Euler
==96649== Memcheck, a memory error detector
==96649== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==96649== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==96649== Command: ./Euler
==96649==
==96649== Invalid read of size 8
==96649==    at 0x400BF2: GInit (Euler.c:85)
==96649==    by 0x400ECB: main (Euler.c:152)
==96649==  Address 0x1786100 is 0 bytes after a block of size 48 alloc'd
==96649==    at 0x100688B: malloc (in /usr/local/lib/valgrind/vgpreload_memcheck-amd64-freebsd.so)
==96649==    by 0x400A8E: GInit (Euler.c:44)
==96649==    by 0x400ECB: main (Euler.c:152)
==96649==
==96649== Invalid write of size 4
==96649==    at 0x400BF8: GInit (Euler.c:85)
==96649==    by 0x400ECB: main (Euler.c:152)
==96649==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==96649==
==96649==
==96649== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==96649==  Access not within mapped region at address 0x0
==96649==    at 0x400BF8: GInit (Euler.c:85)
==96649==    by 0x400ECB: main (Euler.c:152)
==96649==  If you believe this happened as a result of a stack
==96649==  overflow in your program's main thread (unlikely but
==96649==  possible), you can try to increase the size of the
==96649==  main thread stack using the --main-stacksize= flag.
==96649==  The main thread stack size used in this run was 16777216.
==96649==
==96649== HEAP SUMMARY:
==96649==     in use at exit: 32,981 bytes in 16 blocks
==96649==   total heap usage: 16 allocs, 0 frees, 32,981 bytes allocated
==96649==
==96649== LEAK SUMMARY:
==96649==    definitely lost: 0 bytes in 0 blocks
==96649==    indirectly lost: 0 bytes in 0 blocks
==96649==      possibly lost: 0 bytes in 0 blocks
==96649==    still reachable: 32,981 bytes in 16 blocks
==96649==         suppressed: 0 bytes in 0 blocks
==96649== Reachable blocks (those to which a pointer was found) are not shown.
==96649== To see them, rerun with: --leak-check=full --show-reachable=yes
==96649==
==96649== For counts of detected and suppressed errors, rerun with: -v
==96649== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Segmentation fault: 11

我在使用 malloc 时似乎错误地分配了内存。我知道没有释放内存,因为我还没有实现删除功能。 我为 malloc、fgets 和 fscanf 添加了一些额外的测试,以消除可能的错误。 功能GInit应该从文件中读取格式化数据Graph1.txt并创建一个由节点组成的图。文件包含节点数和关联矩阵。

这是我的代码:

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

#define MAXFILENAME 20

typedef struct tNode{
    int Deg;
    int Val;    
    int* Neigh; 
} *tNodePtr;

typedef struct tGraph{
    int Num;    
    tNodePtr* Nodes;    
} *tGraphPtr;


void GInit(tGraphPtr G, const char *FNum)
{
    char FileName[MAXFILENAME];
    char *FileNamePrefix = "Graph";
    char *FileNamePostfix = ".txt";
    FILE *FilePtr;
    int FileBrowser;
    int i, j, k, countNeigh;
    char *line;
    char c;

    strcpy(FileName, FileNamePrefix);
    strcat(FileName, FNum);
    strcat(FileName, FileNamePostfix);

    FilePtr = fopen(FileName, "r");

    if(!FilePtr)
        printf("Can't open file \"%s\"\n", FileName);
    else
    {
        if(!fscanf(FilePtr, "%d", &FileBrowser))
            printf("fscanf error 1!\n");

        G->Num = FileBrowser;
        G->Nodes = malloc(G->Num * sizeof *(G->Nodes));
        if(G->Nodes == NULL)
        {
            printf("Memory allocation error 1!\n");
            return;
        }

        for(i = 0; i < G->Num; i++)
        {
            G->Nodes[i] = malloc(sizeof *(G->Nodes[i]));
            if(G->Nodes[i] == NULL)
            {
                printf("Memory allocation error 2!\n");
                return;
            }
        }

        line = malloc((2*G->Num + 1) * sizeof *line );
        if(line == NULL)
        {
            printf("Memory allocation error 3!\n");
            return;
        }


        i = 0;
        if(!fscanf(FilePtr, "%c", &c))
            printf("fscanf error 2!\n");
        if(fgets(line, 2*G->Num + 1, FilePtr) == NULL)
            printf("fgets error 1!\n"); 
        while(!feof(FilePtr))
        {
            countNeigh = 0;
            j = 0;
            while(line[j] != '\0')
            {
                if(line[j] == '1')
                    countNeigh++;
                j++;
            }

            G->Nodes[i]->Deg = countNeigh;
            G->Nodes[i]->Val = i;
            G->Nodes[i]->Neigh = malloc(countNeigh * sizeof *(G->Nodes[i]->Neigh));
            if(G->Nodes[i]->Neigh == NULL)
            {
            printf("Memory allocation error 4!\n");
            return;
            }


            j = 0;
            k = 0;
            while(line[j] != '\0')
            {
                if(line[j] == '1')
                {
                    G->Nodes[i]->Neigh[k] = j/2;
                    k++;
                }
                j++;
            }

            i++;    
            if(fgets(line, 2*G->Num + 1, FilePtr) == NULL)
                if(i < G->Num)
                    printf("fgets error 2!\n"); 
        }

        free(line);
    }

    fclose(FilePtr);
}

void GPrint(const tGraphPtr G)
{
    int j, k;

    printf("Graph demonstration:\n");
    for(j = 0; j < G->Num; j++)
    {
        printf("I'm Node: %d , my degree is: %d and my neighbours are:\t", G->Nodes[j]->Val, G->Nodes[j]->Deg);
        for(k = 0; k < G->Nodes[j]->Deg; k++)
            printf("%3d", G->Nodes[j]->Neigh[k]);
        printf("\n");
    }
}

void GDelete(tGraphPtr G)
{

}

int main(int argc, char *argv[])
{

    tGraphPtr TmpGraph;
    char *FNum;
    FNum = "1";

    TmpGraph = malloc(sizeof *TmpGraph);
    if(TmpGraph == NULL)
    {
        printf("Memory allocation error 5!\n");
        return -1;
    }

    GInit(TmpGraph, FNum);

    GPrint(TmpGraph);


    return(0);  
}

这是文件Graph1.txt我正在阅读。该文件末尾包含换行符。

6
0 1 0 1 0 0
1 0 1 0 1 1
0 1 0 1 1 1
1 0 1 0 0 0
0 1 1 0 0 0
0 1 1 0 0 0

任何有关如何修复此错误的建议都将受到赞赏。 BTW Microsoft VS2013 成功构建了此代码(当在 malloc 之前使用类型转换时)并且运行没有错误。 谢谢。 约翰


您是否尝试从“Graph.txt”的最后一行中删除换行符并运行二进制文件?

还有,你SHOULD对 valgrind 报告的泄漏采取一些措施。检查这个修改后的代码

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

#define MAXFILENAME 20

typedef struct tNode{
        int Deg;
        int Val;
        int* Neigh;
} *tNodePtr;

typedef struct tGraph{
        int Num;
        tNodePtr* Nodes;
} *tGraphPtr;


void GInit(tGraphPtr G, const char *FNum)
{
        char FileName[MAXFILENAME];
        char *FileNamePrefix = "Graph";
        char *FileNamePostfix = ".txt";
        FILE *FilePtr;
        int FileBrowser;
        int i, j, k, countNeigh;
        char *line;
        char c;

        strcpy(FileName, FileNamePrefix);
        strcat(FileName, FNum);
        strcat(FileName, FileNamePostfix);

        FilePtr = fopen(FileName, "r");

        if(!FilePtr)
                printf("Can't open file \"%s\"\n", FileName);
        else
        {
                if(!fscanf(FilePtr, "%d", &FileBrowser))
                        printf("fscanf error 1!\n");

                G->Num = FileBrowser;
                G->Nodes = calloc(G->Num , sizeof *(G->Nodes));
                if(G->Nodes == NULL)
                {
                        printf("Memory allocation error 1!\n");
                        return;
                }

                for(i = 0; i < G->Num; i++)
                {
                        G->Nodes[i] = malloc(sizeof *(G->Nodes[i]));
                        if(G->Nodes[i] == NULL)
                        {
                                printf("Memory allocation error 2!\n");
                                return;
                        }
                }

                line = malloc((2*G->Num + 1) * sizeof *line );
                if(line == NULL)
                {
                        printf("Memory allocation error 3!\n");
                        return;
                }


                i = 0;
                if(!fscanf(FilePtr, "%c", &c))
                        printf("fscanf error 2!\n");
                if(fgets(line, 2*G->Num + 1, FilePtr) == NULL)
                        printf("fgets error 1!\n");
                while(!feof(FilePtr))
                {
                        countNeigh = 0;
                        j = 0;
                        while(line[j] != '\0')
                        {
                                if(line[j] == '1')
                                        countNeigh++;
                                j++;
                        }
                        G->Nodes[i]->Deg = countNeigh;
                        G->Nodes[i]->Val = i;
                        G->Nodes[i]->Neigh = malloc(countNeigh * sizeof *(G->Nodes[i]->Neigh));
                        if(G->Nodes[i]->Neigh == NULL)
                        {
                                printf("Memory allocation error 4!\n");
                                return;
                        }


                        j = 0;
                        k = 0;
                        while(line[j] != '\0')
                        {
                                if(line[j] == '1')
                                {
                                        G->Nodes[i]->Neigh[k] = j/2;
                                        k++;
                                }
                                j++;
                        }

                        i++;
                        if(fgets(line, 2*G->Num + 1, FilePtr) == NULL)
                                if(i < G->Num)
                                        printf("fgets error 2!\n");
                }

                free(line);
        }

        fclose(FilePtr);
}

void GPrint(const tGraphPtr G)
{
        int j, k;
        int i;

        printf("Graph demonstration:\n");
        for(j = 0; j < G->Num; j++)
        {
                printf("I'm Node: %d , my degree is: %d and my neighbours are:\t", G->Nodes[j]->Val, G->Nodes[j]->Deg);
                for(k = 0; k < G->Nodes[j]->Deg; k++)
                        printf("%3d", G->Nodes[j]->Neigh[k]);
                printf("\n");
        }

        for(i = 0; i < G->Num; i++)
        {
                free (G->Nodes[i]->Neigh);
                free (G->Nodes[i]);
        }

        free (G->Nodes);
}

void GDelete(tGraphPtr G)
{

}

int main(int argc, char *argv[])
{

        tGraphPtr TmpGraph;
        char *FNum;
        FNum = "1";

        TmpGraph = malloc(sizeof *TmpGraph);
        if(TmpGraph == NULL)
        {
                printf("Memory allocation error 5!\n");
                return -1;
        }

        GInit(TmpGraph, FNum);

        GPrint(TmpGraph);

        free (TmpGraph);

        return(0);
}

valgrind 的 o/p

[sourav@localhost ~]$ gcc -g so_test1.c -o test
[sourav@localhost ~]$ valgrind --leak-check=full ./test 
==23941== Memcheck, a memory error detector
==23941== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==23941== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==23941== Command: ./test
==23941== 
Graph demonstration:
I'm Node: 0 , my degree is: 2 and my neighbours are:      1  3
I'm Node: 1 , my degree is: 4 and my neighbours are:      0  2  4  5
I'm Node: 2 , my degree is: 4 and my neighbours are:      1  3  4  5
I'm Node: 3 , my degree is: 2 and my neighbours are:      0  2
I'm Node: 4 , my degree is: 2 and my neighbours are:      1  2
I'm Node: 5 , my degree is: 2 and my neighbours are:      1  2
==23941== 
==23941== HEAP SUMMARY:
==23941==     in use at exit: 0 bytes in 0 blocks
==23941==   total heap usage: 16 allocs, 16 frees, 533 bytes allocated
==23941== 
==23941== All heap blocks were freed -- no leaks are possible
==23941== 
==23941== For counts of detected and suppressed errors, rerun with: -v
==23941== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 12 from 8)
[sourav@localhost ~]$

好吧,我的 Graph.txtDOES NOT末尾有一个换行符。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

大小 8 2 的读/写无效 的相关文章

随机推荐

  • 为什么 Microsoft 的 std::string 实现需要堆栈上的 40 个字节?

    最近看过这个视频 https www youtube com watch v kPR8h4 qZdk关于 facebook 对 string 的实现 我很好奇微软实现的内部原理 不幸的是 字符串文件 在 VisualStudioDirect
  • 我们可以实例化一个抽象类吗?

    在一次采访中 有人问我 我们是否可以实例化一个抽象类 我的回答是 不 我们不能 但是 面试官告诉我 错了 我们可以 我对此争论了一下 然后他告诉我自己在家尝试一下 abstract class my public void mymethod
  • “此 GPIO 引脚已存在:”第二次出现 GPIO 1 异常

    我正在 Raspberry pi 和 java 上工作 通过使用 pi4j 使 LED 闪烁 一切都已清除并且工作正常 LED 按照代码闪烁 但是当我第二次运行时 它会导致以下错误 我已经搜索了很多有很多相同的问题没有明确的答案如何解决 任
  • 键入时自动滚动 DataGridView

    我遇到这个问题 DataGridView 中的最后一列太长 您需要使用滚动条来显示该列的其余部分 但是当我输入文本时 它不会在输入时自动滚动 我想要的是 我想在打字时自动滚动滚动条 以便用户在打字时不必使用滚动条 这是图像 As you c
  • 通过插件管理器在 Notepad++ 中配置代理设置

    我想在 Notepad 中配置代理设置 以允许通过代理从互联网下载 在从网上搜索如何执行此操作后 我了解到我需要通过 设置 按钮在插件管理器中执行此操作 当我转到插件 gt 插件管理器 gt 显示插件管理器 gt 设置时 我看到下面的对话框
  • 字典查找抛出“索引超出数组范围”

    我收到了一个错误报告 该报告似乎来自以下代码 public class AnimationChannelCollection ReadOnlyCollection
  • 使用Unity使用什么语言进行开发

    使用 Unity 进行编程时需要使用什么语言 或者它是多种语言的API 我通读了文档 我想我错过了所使用的语言的要点 它说它有 iOS 部署 这仍然允许程序员使用 Objective C 进行编码吗 Unity 是一个可用于多种平台的 sd
  • jQuery 插件与小部件

    几个月前 我开始使用 jQuery 插件进行一些实验 我在互联网上找到了一些教程 然后开始整理一些东西 几天前 我需要构建自己的 插件 并回到我的旧项目 当我试图在互联网上找到更多信息时 我偶然发现了这些称为小部件的新 东西 据我了解 我应
  • 如何在选中单元格后停止 DataGridView 编辑?

    I use ContexMenuStrip on DataGridView删除一些行 但它无法正常工作 每次如果我检查 3 行 选择后ContexMenuStrip它只删除 2 行 当我在没有的情况下执行此代码时ContexMenuStri
  • 编写一个加载 msvcr80.dll 并公开 free() 函数的 DLL

    我有一个依赖于 MSVCR80 的第三方 DLL 并分配我需要清理的资源 图书馆有not暴露一个free 执行此操作的函数 相反 我需要加载相同的运行时库并手动调用free功能 作为一种解决方法 我尝试编写一个 包装器 DLL 它加载正确的
  • 导航到特定的数据透视项

    当我点击主页上的图像时 如何导航到数据透视表的特定数据透视项 主页上的图像的 XAML 代码如下
  • UISearchBar 取消按钮没有响应

    我已经实现了搜索栏 一旦用户将焦点放在搜索栏中 它就会显示取消按钮 为此我写了searchBar showsCancelButton YES in my searchBarTextDidBeginEditing方法 在searchBarSe
  • 不允许从此上下文调用 Browser.inputBox()

    在我第一次使用 Google 脚本时 我尝试定义一个调用 Browser input 方法来获取用户名的函数 但我总是收到以下错误消息 不允许从此上下文中调用 Browser inputBox 我必须说 我是从葡萄牙语环境翻译的 因为英语中
  • 为什么签名的 Android apk 无法在模拟器上运行

    我已经制作了一个android项目的签名apk 每当我的客户尝试在模拟器上运行它时 他都会遇到以下错误消息 D Android android sdk windows tools gt adb install r abc apk 500 K
  • Draggable 正在阻止触摸事件

    我正在尝试使用拖动来来回移动 div 这部分工作正常 直到 div 具有可滚动内容 由于滚动条 这在桌面上不是问题 但在触摸设备上会出现问题 由于触摸事件与拖动事件冲突 我无法滚动内容 我尝试创建一个条件来检测拖动是否水平方向多于垂直方向
  • 将 NSString 分离成 N​​SArray,但允许用引号对单词进行分组

    我有一个搜索字符串 人们可以使用引号将短语组合在一起 并将其与单个关键字混合 例如 像这样的字符串 Something amazing rooster 我想把它分成一个 NSArray 这样它就有Something amazing 不带引号
  • 的 CSS margin-top 影响父级的边距

    我已经研究这个问题有一段时间了 但还没有找到直接的答案 当向元素添加页边距顶部时 就我而言 它主要发生在标题上 在许多情况下 边距顶部是与父级共享的 HTML div h1 My title h1 div CSS div padding 2

  • 传递多个参数或对象(单击)

    问题是将对象或多个参数从模板传递到组件 并使用它们将数据添加到 API 任务 service ts addTasks task Task Observable
  • ESLint Airbnb ES6 和 Redux 异步操作围绕箭头主体的意外块语句

    我究竟做错了什么 我有其他三个异步操作也有同样的问题并且无法修复它 当你看一眼箭头函数文档 https developer mozilla org en docs Web JavaScript Reference Functions Arr
  • 大小 8 2 的读/写无效

    在处理我的学校项目时 在 Unix 学校服务器上编译我的项目后 我不断收到来自 Valgrind 的以下错误 并且无法运行该程序 因为我收到 分段错误 11 95183 Memcheck a memory error detector 95