【发布时间】:2017-10-23 17:44:20
【问题描述】:
在图像中的单个字母模式中,我编写了一个小函数,该函数将从任何像素开始并遍历所有连续像素。简而言之,在矩阵中,它会将所有连续像素打开为真,其余像素为零。
此函数已正确完成。然而,输入模式几乎没有变化,它的行为异常。我发现这条线以后://process left-up diagonally 没有被调用。
可能是什么原因?
valgrind 也没有显示内存损坏。输入的 jpg 文件大小最大为 170x30 像素
系统 Ubuntu-16
生成文件:
CFLAGS= -O2 -c -I$(INC_DIR) -fpermissive -std=c++11
CC=g++-5
%.o: %.cpp
$(CC) $(CFLAGS) -c $< -o $@
readpattern: readpattern.o
g++ -IPNG/include -o readpattern readpattern.o libcorona.a -lpng -ljpeg
代码
void do_start_extract_char(char **output, int x, int y, int width, int height) {
//if pixel location croses boundary then return
if (x < 0 || y < 0 || x > width - 1 || y > height - 1) {
return;
}
//if the same pixel has already been visited then just return
if (output[y][x]) {
return;
}
if (isUsefulPixel(x, y)) {
//store it
output[y][x] = 1;
} else {
return;
}
//process left
do_start_extract_char(output, x - 1, y, width, height);
//process down
do_start_extract_char(output, x, y + 1, width, height);
//process right
do_start_extract_char(output, x + 1, y, width, height);
//process up
do_start_extract_char(output, x, y - 1, width, height);
//process left-down diagonally
// /
// /
do_start_extract_char(output, x - 1, y + 1, width, height);
//process left-up diagonally
// \
// \
do_start_extract_char(output, x - 1, y - 1, width, height);
//process right-down diagonally
// \
// \
do_start_extract_char(output, x + 1, y + 1, width, height);
//process right-up diagonally
// /
// /
do_start_extract_char(output, x + 1, y - 1, width, height);
return;
}
【问题讨论】:
-
@user5858 在使用调试器单步执行代码时,您观察到了什么?关于您的代码,我没有发现任何错误,这很明显。
-
是时候创建一个minimal reproducible example了。如果创建 MCVE 并不能完全暴露问题并让您修复它,请编辑您的问题并添加 MCVE。
-
唾手可得的果实:确保您没有用完自动存储。
-
@user4581301 我怎么知道这个?不过我从来没有遇到过这种自动存储问题
-
仔细阅读 Yakk 的回答。它涵盖了这个问题。每个函数调用最多可以假设 64 位是您可用的最好的)40 字节的存储空间。对于 1 百万像素的图像,40 MB 的存储空间可能会在你结束时被吃掉,而且大多数系统都是基于堆栈的,具有 2 到 10 MB 的存储空间