- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下代码,它应该使用 calloc 分配宽度和高度 imageWidth 的二维数组(它恰好用于玩具四叉树构建程序)。第三个调试输出是跟踪在循环过程中 image[0] 到 [10] 数组中发生的事情。
/* allocate pointer array memory */
char** image = calloc (imageWidth, 1);
if (image == NULL) errMalloc();
/* fill with a series of char arrays */
for (i = 0; i < imageWidth; i++) {
image[i] = calloc (imageWidth, 1);
if (image[i] == NULL) errMalloc();
/* debug prints */
printf("%d ", i);
printf("%d ", image[i][0]);
printf("%d\n", image[i%10][0]);
}
当图像宽度小于 ~20(比如 16)时,我会得到预期的打印效果,例如
0 0 0
1 0 0
2 0 0
etc...
15 0 0
但是将 imageWidth 设置为 29 会产生类似于
0 0 0
1 0 0
etc...
9 0 0
10 0 16 //value at image [0][0] has changed
11 0 0
etc...
19 0 0
20 0 16
21 0 -96 // now value at image[1][0] has changed
22 0 0
etc..
27 0 0
28 0 0
这可能是什么原因造成的?我非常怀疑 calloc 在再次调用时会改变其他内存中的值,所以错误一定在我的代码中。如果有用的话,这两个 if 语句只会导致 puts() 和 exit()。当我得到奇怪的结果时,他们没有输入。
谢谢!
最佳答案
第一次分配应该是:
char** image = calloc (imageWidth, sizeof *image);
因为您分配的是“imageWidth”指针数,而不是字节数。
关于反复调用 calloc 似乎会破坏早期调用的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42855895/
我有一个小心理障碍:我有一个 html 中的 javascript 函数,可以在按下某个键时写入键码。我已经将它连接到一个函数,该函数可以准确地告诉我按下了什么字符和键码(不包括在内)。 我的问题是如
我上网已经有一段时间了,现在正试图解决这个问题。可以找到正在发生的事情的视频: https://i.gyazo.com/59fc489b6099b513c41aedeed482b8d2.mp4 $(d
我有一个 Python 程序,其中一个函数导入另一个脚本并运行它。但是脚本仅在第一次函数被调用时运行。 def Open_Generator(事件): 导入密码生成器 有什么建议吗? *该函数在 tk
我正在尝试在 git 中创建两个包含二进制文件的分支 - 一个“开发”分支和一个“稳定”分支。在我想将它们“发布”到稳定分支之前,开发分支可以对这些文件进行一些更改(并且稳定分支已重命名这些文件,以防
我在 IBM BlueMix 中的 Node Red 应用程序反复崩溃 - 每小时一次 - 除了“exited with status: 1.”之外没有任何真正的错误消息 如何解决此问题? 是否有来自
我是一名优秀的程序员,十分优秀!