- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个迷宫问题程序。我可以成功地获取路径的方向。然而,我确实迷失了方向,并从 valgrind 报告中发现了错误。我的代码有什么问题?
以下是我的代码:
002: #include <stdio.h>
003: #include <stdlib.h>
004: #include <string.h>
005:
006: struct each_path {
007: int step_nums, coin_nums;
008: char **step_dir;
009: };
010:
011: void initial_value();
012: void *readFile(char *fileName);
013: void maze_1Dto2D(char *array);
014: void visit(int, int);
015: void display_direction();
016: struct each_path *epath;
017: int path_assume_num;
018: char *maze1D, **maze2D, **maze2D_tmp;
019: int maze_height, maze_width;
020: int startI = 1, startJ = 1, endI, endJ; // entrance & exit
021: int path_nums, coin_nums, min_step_num, min_path_num;
022:
023:
024: int main(void) {
025: char *maze_txtfile;
026: int i, j;
027:
028: // the first maze
029: initial_value();
030: maze_txtfile = readFile("maze2.txt");
031: maze2D_tmp = malloc( maze_height * sizeof(*maze2D_tmp));
032: for ( i = 0; i < maze_height; ++i ) {
033: maze2D_tmp[i] = malloc(maze_width + 1);
034: memcpy(maze2D_tmp[i], maze2D[i], maze_width + 1);
035: }
036: epath = malloc(path_assume_num * sizeof(*epath));
037: for ( i = 0; i < path_assume_num; ++i) {
038: epath[i].step_dir = malloc(10 * sizeof(*epath[i].step_dir));
039: for ( j = 0; j < 10; ++j)
040: epath[i].step_dir[j] = malloc(6);
041: }
042:
043: endI = maze_height - 2;
044: endJ = maze_width - 2;
045: visit(startI, startJ);
046: display_direction();
047:
048: for ( i = 0; i < path_nums; ++i) {
049: for ( j = 0; j < epath[i].step_nums; ++j)
050: free(epath[i].step_dir[j]);
051: free(epath[i].step_dir);
052: }
053: for ( i = 0; i < maze_height; ++i) {
054: free(maze2D[i]);
055: free(maze2D_tmp[i]);
056: }
057: free(maze1D);
058: free(maze2D);
059: free(maze2D_tmp);
060: free(epath);
061:
062: exit(0);
063: }
064:
065: void initial_value() {
066: path_assume_num = 1;
067: maze_height = 0;
068: maze_width = 0;
069: path_nums = 0;
070: coin_nums = 0;
071: min_step_num = 100000;
072: }
073:
074:
075: void *readFile(char *fileName) {
076: FILE *file = fopen(fileName, "r");
077: size_t maze_unit_num = 0, maze_assume_size = 100;
078: int maze_unit;
079:
080: if (file == NULL)
081: return NULL; //could not open file
082:
083: maze1D = malloc(maze_assume_size);
084:
085: while ((maze_unit = fgetc(file)) != EOF)
086: {
087: if (maze_unit_num >= maze_assume_size)
088: {
089: maze_assume_size *= 2;
090: maze1D = realloc(maze1D, maze_assume_size);
091: }
092: maze1D[maze_unit_num] = (char) maze_unit;
093:
094: if (maze1D[maze_unit_num] == '\n')
095: {
096: if (maze_height == 0)
097: maze_width = maze_unit_num - 1;
098: maze_height++;
099: }
100: maze_unit_num++;
101: }
102: maze1D = realloc(maze1D, maze_unit_num + 1);
103: maze1D[maze_unit_num] = '\0';
104: maze_1Dto2D(maze1D);
105: }
106:
107: void maze_1Dto2D(char *array) {
108: size_t i = 0, j = 0, num = 0;
109: maze2D = malloc( maze_height * sizeof(*maze2D));
110:
111: for ( i = 0; i < maze_height ; ++i)
112: {
113: maze2D[i] = malloc(maze_width + 1);
114: for ( j = 0; j < maze_width + 1; ++j, ++num)
115: {
116: maze2D[i][j] = array[num];
117:
118: if (array[num] == '\r')
119: --j;
120: else if (array[num] == '\n')
121: maze2D[i][j] = '\0';
122: else
123: maze2D[i][j] = array[num];
124: }
125: }
126: }
127:
128: void visit(int i, int j) {
129: int preI, preJ, curI = 1, curJ = 1;
130: int step_nums = 0, step_assume_num = 10; // entrance is not included
131: char dir[6];
132: int m, n;
133:
134: if (maze2D_tmp[i][j] == '2')
135: coin_nums++;
136: maze2D_tmp[i][j] = '3';
137: if (i == endI && j == endJ) {
138: if (path_nums >= path_assume_num) {
139: path_assume_num *= 2;
140: epath = realloc(epath, path_assume_num * sizeof(*epath));
141: for ( m = path_assume_num / 2; m < path_assume_num; ++m) {
142: epath[m].step_dir = malloc(10 * sizeof(*epath[m].step_dir));
143: for ( n = 0; n < 10; ++n)
144: epath[m].step_dir[n] = malloc(6);
145: }
146: }
147: while (curI != endI || curJ != endJ)
148: {
149: if (step_nums >= step_assume_num) {
150: step_assume_num *= 2;
151: epath[path_nums].step_dir = realloc(epath[path_nums].step_dir, step_assume_num * sizeof(*epath[path_nums].step_dir));
152: for ( m = step_assume_num / 2; m < step_assume_num; ++m)
153: epath[path_nums].step_dir[m] = malloc(6);
154: }
155:
156: if ( maze2D_tmp[curI][curJ + 1] == '3' && preJ != (curJ + 1) ) {
157: preI = curI;
158: preJ = curJ;
159: curJ++;
160: strcpy(dir, "right");
161:
162: }
163: else if ( maze2D_tmp[curI + 1][curJ] == '3' && preI != (curI + 1) ) {
164: preI = curI;
165: preJ = curJ;
166: curI++;
167: strcpy(dir, "down");
168: }
169: else if ( maze2D_tmp[curI - 1][curJ] == '3' && preI != (curI - 1) ) {
170: preI = curI;
171: preJ = curJ;
172: curI--;
173: strcpy(dir, "up");
174: }
175: else if ( maze2D_tmp[curI][curJ - 1] == '3' && preJ != (curJ - 1)) {
176: preI = curI;
177: preJ = curJ;
178: curJ--;
179: strcpy(dir, "left");
180: }
181: strcpy(epath[path_nums].step_dir[step_nums], dir);
182: step_nums++;
183: }
184: epath[path_nums].step_dir = realloc(epath[path_nums].step_dir, step_nums * sizeof(*epath[path_nums].step_dir));
185: epath[path_nums].step_nums = step_nums;
186: epath[path_nums].coin_nums = coin_nums;
187: path_nums++;
188: if (step_nums < min_step_num)
189: {
190: min_step_num = step_nums;
191: min_path_num = path_nums;
192: }
193: }
194:
195: if (maze2D_tmp[i][j + 1] == '1' || maze2D_tmp[i][j + 1] == '2') visit(i, j + 1);
196: if (maze2D_tmp[i + 1][j] == '1' || maze2D_tmp[i + 1][j] == '2') visit(i + 1, j);
197: if (maze2D_tmp[i][j - 1] == '1' || maze2D_tmp[i][j - 1] == '2') visit(i, j - 1);
198: if (maze2D_tmp[i - 1][j] == '1' || maze2D_tmp[i - 1][j] == '2') visit(i - 1, j);
199:
200: if (maze2D[i][j] == '2')
201: {
202: maze2D_tmp[i][j] = '2';
203: coin_nums--;
204: }
205: else
206: maze2D_tmp[i][j] = '1';
207: }
208:
209: void display_direction() {
210: int i;
211: for ( i = 0; i < min_step_num; ++i)
212: printf("%s\n", epath[min_path_num - 1].step_dir[i]);
213: printf("coin numbers:%d\n", epath[min_path_num - 1].coin_nums);
214: }
瓦尔格林报告:
我从泄漏摘要中得到“肯定丢失:318 个 block 中的 1,908 字节”,并且错误摘要中来自 4 个上下文的 4 个错误(已抑制:0 来自 0)。哪些因素可能导致这些错误消息?
==3410== HEAP SUMMARY:
==3410== in use at exit: 2,460 bytes in 319 blocks
==3410== total heap usage: 742 allocs, 423 frees, 37,249 bytes allocated
==3410==
==3410== 954 bytes in 159 blocks are definitely lost in loss record 2 of 3
==3410== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3410== by 0x401007: visit (4thesecondmaze.c:153)
==3410== by 0x401352: visit (4thesecondmaze.c:196)
==3410== by 0x401352: visit (4thesecondmaze.c:196)
==3410== by 0x401352: visit (4thesecondmaze.c:196)
==3410== by 0x401352: visit (4thesecondmaze.c:196)
==3410== by 0x401352: visit (4thesecondmaze.c:196)
==3410== by 0x401352: visit (4thesecondmaze.c:196)
==3410== by 0x401352: visit (4thesecondmaze.c:196)
==3410== by 0x4012EC: visit (4thesecondmaze.c:195)
==3410== by 0x4012EC: visit (4thesecondmaze.c:195)
==3410== by 0x4012EC: visit (4thesecondmaze.c:195)
==3410==
==3410== 954 bytes in 159 blocks are definitely lost in loss record 3 of 3
==3410== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3410== by 0x401007: visit (4thesecondmaze.c:153)
==3410== by 0x4012EC: visit (4thesecondmaze.c:195)
==3410== by 0x401352: visit (4thesecondmaze.c:196)
==3410== by 0x4013BA: visit (4thesecondmaze.c:197)
==3410== by 0x401352: visit (4thesecondmaze.c:196)
==3410== by 0x401352: visit (4thesecondmaze.c:196)
==3410== by 0x401352: visit (4thesecondmaze.c:196)
==3410== by 0x401352: visit (4thesecondmaze.c:196)
==3410== by 0x401352: visit (4thesecondmaze.c:196)
==3410== by 0x401352: visit (4thesecondmaze.c:196)
==3410== by 0x4012EC: visit (4thesecondmaze.c:195)
==3410==
==3410== LEAK SUMMARY:
==3410== definitely lost: 1,908 bytes in 318 blocks
==3410== indirectly lost: 0 bytes in 0 blocks
==3410== possibly lost: 0 bytes in 0 blocks
==3410== still reachable: 552 bytes in 1 blocks
==3410== suppressed: 0 bytes in 0 blocks
==3410== Reachable blocks (those to which a pointer was found) are not shown.
==3410== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)
最佳答案
在第 153 行,您将一堆内存块分配到 step_dir
中,并最终获得 step_assume_num
分配的 block 。
稍后,在第 184 行,您调用 realloc
将 step_dir
分配的内存减少到实际使用的数量,即 step_num
block 。已分配但未使用的 block (从 step_num + 1
到 step_assume_num
)尚未释放。您需要在减小数组大小之前释放这些 block 。
关于c - Valgrind:C 程序中肯定丢失内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47362651/
我在 Ubuntu 10.04 LTS 上运行 Eclipse Galileo。今天 Ubuntu 在我身上崩溃了,重新启动后,我发现 Eclipse 已经完全失去了 Java Perspective
我使用配置了 sonata_user 的 SonataAdminBundle在 config.yml : sonata_user: impersonating: route:
我有 ubuntu 14.04 但它不见了 docker exec sudo docker exec -it ubuntu_bash bash 我希望在现有正在运行的 docker 容器中运行交互式
我正在使用 Ubuntu 8.04/32 位(作为虚拟机)。在一个不是 min 的项目上执行一些 make 时,我得到了错误: g++:/usr/lib/libstdc++.a: 没有这样的文件或目录
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许在 Stack Overflow 上提出有关通用计算硬件和软件的问题。您可以编辑问题,使其成为
我正在尝试获取有关我在 UIImagePicker 中选择的视频的一些数据。 因此,当它进入 UIImagePicker 委托(delegate)方法(如下)时,我知道我需要使用信息字典中的 UIIm
我的网站最近被可能的黑客行为删除了。我上传了备份的文件夹和数据库,但现在我的 View 没有显示。其他一切都有效。我想不出有什么变化,只是上传了几天前的备份。 这些字段在 admin/build/vi
我执行以下操作来设置我的 session ,这是有效的,因为 echo 出现了。但是当我转到下一页或另一页时, session 不存在吗?我做错了什么? $session_start(); if ($
我试图在 BigQuery 中使用这段代码,显然是从 GA 中获取数据,但 _TABLE_SUFFIX 似乎有问题。错误显示“错误:无法识别的名称:_TABLE_SUFFIX at [12:3]”您能
输入:8(2 5 6 9 10 2 7 4)预期输出:(2 7 9 9 12 5 7 6)实际输出:(2 7 9 9) 这是我的大学作业,也是我第一次在这里提问。我不知道为什么,但 10 没有扫描,有
$('div'); // 我在上面的代码中遇到错误。在检查 .js 代码时,我找不到名为 $ 的函数,但根据文档,应该有一个。 最佳答案 试试 http://ajax.googleapis.co
以下简单代码的输出对我来说有点奇怪。它错过了在控制台上打印的 0 到 100 之间的一些数字。 谁能解释一下为什么省略打印?我对并发编程完全陌生。 import java.util.concurren
我正在学习 xamarin 以构建移动应用程序,但我对 Xamarin 和移动应用程序一无所知。我打开了一个空白的移动应用程序,其中有“Hello world”示例并编辑了一些文本,但我的应用程序图标
我正在将一些值存储到 sqlite 数据库中。因此,数据是作为字符串从文本字段收集的,然后转换为 double 并持久化。 这是我试过的 NSDecimalNumber 答案; value.answe
我有一个奇怪的案例。突然,其中一个表中的一些记录(这么多记录)丢失了。首先,我认为这是由我的 PHP 脚本中的错误引起的。但是,我检查了一下,我的脚本中没有DELETE操作,只有UPDATE。有谁知道
我正在复制 NSString来自 NSDictionary进入本地NSString使用 [[NSString alloc] initWithString:] ,对其进行处理(删除一些字符),然后将其发
当保存在根文件夹中时,我的非常基本的 html 页面保持样式。为一个组创建了一个新文件夹,但是当我将页面移动到该文件夹时,它们似乎失去了与 css 文件的连接。 认为问题可能出在链接上,因为它现在
我需要使用 OpenCV 训练一些图像。但问题是,我找不到 opencv_createsamples 程序。我以正常方式安装了 OpenCV,因为我使用的是 Windows 7。这个 opencv_c
我有一个绑定(bind)到 JTable 的 ArrayList。之后 bindingGroup.unbind(); bindingGroup.bind(); (完成刷新数据)我丢失了表格单元格渲
所以基本上我们只是丢失了一个 keystore 和备份 keystore 。但是我们可能知道原始 keystore 的密码。 我的问题是,如果我们知道原始 keystore 的密码,我们可以重新生成
我是一名优秀的程序员,十分优秀!