ApplicationName.exe 已触发 Visual Studio 中的断点

如何解决ApplicationName.exe 已触发 Visual Studio 中的断点

typedef struct Tuple
{
    int row;
    int col;
    int data;
} Tuple;

int** createMatrix(int r,int c)
{
    int** mat = (int**)malloc(sizeof(int*) * r);
    for (int i = 0; i < r; i++)
        mat[i] = malloc(sizeof(int) * c);
    return mat;
}

Tuple* createTuple(int** mat,int r,int c)
{
    int count = 0;
    for (int i = 0; i < r; i++)
        for (int j = 0; j < c; j++)
            if (mat[i][j])
                count++;

    Tuple* tuple = (Tuple*)malloc(sizeof(Tuple) * count);
    tuple[0].col = c;
    tuple[0].row = r;
    tuple[0].data = count;
    int k = 1;
    for (int i = 0; i < r; i++)
        for (int j = 0; j < c; j++)
            if (mat[i][j])
            {
                tuple[k].col = j;
                tuple[k].row = i;
                tuple[k++].data = mat[i][j];
            }
    return tuple;
}

Tuple* fastTranspose(Tuple* tuple)
{
    Tuple* trans = (Tuple*)calloc((tuple[0].data + 1),sizeof(Tuple) );
    trans[0].col = tuple[0].row;
    trans[0].row = tuple[0].col;
    trans[0].data = tuple[0].data;
    int* rowTerms = (int*)malloc(sizeof(int) * tuple[0].col);
    int* startingPos = (int*)malloc(sizeof(int) * tuple[0].col);
    for (int i = 0; i < tuple[0].col; i++)
        rowTerms[i] = 0;
    for (int i = 1; i <= tuple[0].data; i++)
        rowTerms[tuple[i].col]++;
    startingPos[0] = 1;
    for (int i = 1; i < tuple[0].col; i++)
        startingPos[i] = startingPos[i - 1] + rowTerms[i - 1];

    for (int i = 1; i <= tuple[0].data; i++)
    {
        int j = startingPos[tuple[i].col]++;
        trans[j].col = tuple[i].row;
        trans[j].row = tuple[i].col;
        trans[j].data = tuple[i].data;
    }
    free(rowTerms);
    free(startingPos);
    return trans;
}

void printTuple(Tuple* tuple)
{
    printf("\nRow Col Val:\n");
    for (int i = 0; i <= tuple[0].data; i++)
    {
        printf("%3d%4d%4d\n",tuple[i].row,tuple[i].col,tuple[i].data);
    }
}

int main()
{
    int** mat = createMatrix(3,4);
    printf("Enter data:\n");
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 4; j++)
            scanf("%d",&mat[i][j]);
    Tuple* tuple = createTuple(mat,3,4);
    printf("Original tuple:");
    printTuple(tuple);
    Tuple* trans = fastTranspose(tuple);
    printf("Tuple transpose:");
    printTuple(trans);
    free(trans);
    free(tuple);
    for (int i = 0; i < 3; i++)
        free(mat[i]);
    free(mat);
}

为什么我没有在函数fastTranspose的第一行设置断点?

我查看了类似的问题,但找不到解决方案。它是一个求矩阵转置的程序。我们首先动态分配一个二维矩阵。然后将其转换为稀疏矩阵表示形式,该形式使用结构来存储行、列和数据的值。 我尝试打印矩阵和元组的值。有效。但是进入快速转置功能时突然触发断点。

我们通常会自己在visual studio中放置一个断点。为什么是IDE放的?

解决方法

Visual Studio 在那里放置了一个断点很有帮助,因为您在那个地方遇到了错误。如果我在最近的 Linux (Mint 20) 和最近的 clang (12.0.0) 上执行此操作,我会得到以下输出:

$ echo -e '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12' | ./so_tmat 
Enter data:
Original tuple:
Row Col Val:
  3   4  12
  0   0   1
  0   1   2
  0   2   3
  0   3   4
  1   0   5
  1   1   6
  1   2   7
  1   3   8
  2   0   9
  2   1  10
  2   2  11
  2   3  12
tuple[0].data + 1 = 13,sizet(Tuple) = 12
so_tmat: malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted (core dumped)

GDB(GNU 调试器)的回溯指向与 VS 相同的行。

如果我(你的里程数可能会有所不同,因为:谷歌)将它原样复制并粘贴到谷歌中,我得到了 Why do I get a C malloc assertion failure? 作为第一个命中。接受的答案以正确的方式指出,我们将向 Valgrind 寻求一些提示。

$ echo -e '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12' |  valgrind  --track-origins=yes --leak-check=full --show-leak-kinds=all  ./so_tmat 
==16047== Memcheck,a memory error detector
==16047== Copyright (C) 2002-2017,and GNU GPL'd,by Julian Seward et al.
==16047== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==16047== Command: ./so_tmat
==16047== 
Enter data:
==16047== Invalid write of size 8
==16047==    at 0x40144D: createTuple (so_tmat.c:38)
==16047==    by 0x40144D: main (so_tmat.c:89)
==16047==  Address 0x4a796a0 is 0 bytes after a block of size 144 alloc'd
==16047==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==16047==    by 0x401377: createTuple (so_tmat.c:28)
==16047==    by 0x401377: main (so_tmat.c:89)
==16047== 
==16047== Invalid write of size 4
==16047==    at 0x401452: createTuple (so_tmat.c:39)
==16047==    by 0x401452: main (so_tmat.c:89)
==16047==  Address 0x4a796a8 is 8 bytes after a block of size 144 alloc'd
==16047==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==16047==    by 0x401377: createTuple (so_tmat.c:28)
==16047==    by 0x401377: main (so_tmat.c:89)
==16047== 

    ...

我们可以停在这里,因为我们找到了原因:您有缓冲区溢出,因为您在 count = 0 中以 createMatrix(int r,int c) 开头,这导致缺少最后一个 Tuple 所需的内存.将其更改为 count = 1,它应该可以工作。

让我们检查一下:

$ echo -e '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12' |  valgrind  --track-origins=yes --leak-check=full --show-leak-kinds=all  ./so_tmat 
==16215== Memcheck,a memory error detector
==16215== Copyright (C) 2002-2017,by Julian Seward et al.
==16215== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==16215== Command: ./so_tmat
==16215== 
Enter data:
Original tuple:
Row Col Val:
  3   4  13
  0   0   1
  0   1   2
  0   2   3
  0   3   4
  1   0   5
  1   1   6
  1   2   7
  1   3   8
  2   0   9
  2   1  10
  2   2  11
  2   3  12
==16215== Invalid read of size 4
==16215==    at 0x4014A0: printTuple (so_tmat.c:78)
==16215==    by 0x4014A0: main (so_tmat.c:91)
==16215==  Address 0x4a796b4 is 8 bytes after a block of size 156 alloc'd
==16215==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==16215==    by 0x40137A: createTuple (so_tmat.c:28)
==16215==    by 0x40137A: main (so_tmat.c:89)

    ...

还有一个问题,现在是在printTuple(Tuple* tuple)中,因为我们改变了count的初始化,但是没有调整tuple[0].data中的值,所以for (int i = 0; i <= tuple[0].data; i++)行的上限{1}} 太大。在这里或那里更正它,并不重要,我会更正循环中的上限,因为 tuple[0].data 包含正确数量的非零条目。因此将循环更改为 for (int i = 0; i < tuple[0].data; i++)

再次检查:

$ echo -e '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12' |  valgrind  --track-origins=yes --leak-check=full --show-leak-kinds=all  ./so_tmat 
==16328== Memcheck,a memory error detector
==16328== Copyright (C) 2002-2017,by Julian Seward et al.
==16328== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==16328== Command: ./so_tmat
==16328== 
Enter data:
Original tuple:
Row Col Val:
  3   4  13
  0   0   1
  0   1   2
  0   2   3
  0   3   4
  1   0   5
  1   1   6
  1   2   7
  1   3   8
  2   0   9
  2   1  10
  2   2  11
  2   3  12
tuple[0].data + 1 = 14,sizet(Tuple) = 12
==16328== Invalid read of size 4
==16328==    at 0x4015B1: fastTranspose (so_tmat.c:56)
==16328==    by 0x4015B1: main (so_tmat.c:92)
==16328==  Address 0x4a796b0 is 4 bytes after a block of size 156 alloc'd
==16328==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==16328==    by 0x40137A: createTuple (so_tmat.c:28)
==16328==    by 0x40137A: main (so_tmat.c:89)
==16328== 
==16328== Invalid read of size 4
==16328==    at 0x4017D0: fastTranspose (so_tmat.c:63)
==16328==    by 0x4017D0: main (so_tmat.c:92)
==16328==  Address 0x4a796b0 is 4 bytes after a block of size 156 alloc'd
==16328==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==16328==    by 0x40137A: createTuple (so_tmat.c:28)
==16328==    by 0x40137A: main (so_tmat.c:89)
==16328== 
==16328== Invalid read of size 4
==16328==    at 0x4017DF: fastTranspose (so_tmat.c:64)
==16328==    by 0x4017DF: main (so_tmat.c:92)
==16328==  Address 0x4a796ac is 0 bytes after a block of size 156 alloc'd
==16328==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==16328==    by 0x40137A: createTuple (so_tmat.c:28)
==16328==    by 0x40137A: main (so_tmat.c:89)
==16328== 
==16328== Invalid read of size 4
==16328==    at 0x4017ED: fastTranspose (so_tmat.c:66)
==16328==    by 0x4017ED: main (so_tmat.c:92)
==16328==  Address 0x4a796b4 is 8 bytes after a block of size 156 alloc'd
==16328==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==16328==    by 0x40137A: createTuple (so_tmat.c:28)
==16328==    by 0x40137A: main (so_tmat.c:89)

   ...

查看有问题的行(您的行号可能不同):与之前相同的上限问题。更改并再次检查。

$ echo -e '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12' |  valgrind  --track-origins=yes --leak-check=full --show-leak-kinds=all  ./so_tmat 
==16420== Memcheck,a memory error detector
==16420== Copyright (C) 2002-2017,by Julian Seward et al.
==16420== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==16420== Command: ./so_tmat
==16420== 
Enter data:
Original tuple:
Row Col Val:
  3   4  13
  0   0   1
  0   1   2
  0   2   3
  0   3   4
  1   0   5
  1   1   6
  1   2   7
  1   3   8
  2   0   9
  2   1  10
  2   2  11
  2   3  12
tuple[0].data + 1 = 14,sizet(Tuple) = 12
Tuple transpose:
Row Col Val:
  4   3  13
  0   0   1
  0   1   5
  0   2   9
  1   0   2
  1   1   6
  1   2  10
  2   0   3
  2   1   7
  2   2  11
  3   0   4
  3   1   8
  3   2  12
==16420== 
==16420== HEAP SUMMARY:
==16420==     in use at exit: 0 bytes in 0 blocks
==16420==   total heap usage: 10 allocs,10 frees,5,548 bytes allocated
==16420== 
==16420== All heap blocks were freed -- no leaks are possible
==16420== 
==16420== For lists of detected and suppressed errors,rerun with: -s
==16420== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

很好,没有问题了。至少对于那个确切的输入。但是我在 printf("tuple[0].data + 1 = %d,sizet(Tuple) = %zu\n",tuple[0].data + 1,sizeof(Tuple) ); 之前挤了一点 calloc(),它说

tuple[0].data + 1 = 14,sizet(Tuple) = 12

那太多了Tuple;节省一些内存并通过删除 + 1 中的 calloc 来更改它。就是这样。

我在这里使用了 Unix 工具,其中大部分都可用于 Windows,但 VS 也有所有必要的工具,它们只是名称不同,使用方式不同。尝试找到它们并与它们一起检查您的原始代码(您现在知道其中的错误)以熟悉该工具链。您真的需要这些知识!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-