递归期间代码执行中断

Code execution breaking during recursion

本文关键字:执行 中断 代码 递归      更新时间:2023-10-16

在图像中的单个字母模式中,我编写了一个小函数,它将从任何像素开始并遍历所有连续像素。简而言之,在矩阵中,它将把所有连续像素打开为 true,其余像素将为零。

此函数已正确完成。然而,由于输入模式几乎没有变化,它的行为异常。我发现这条线继续://process left-up diagonally没有被召唤。

可能是什么原因?

此外,valgrind 没有显示内存损坏。输入 jpg 文件大小最大为 170x30 像素

系统乌班图-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;
}

从大多数像素开始,递归地向左、向下、向右和向上移动足以覆盖整个图像中的每个像素。

下像素只是当无法通过左、下、右和上到达像素时到达像素的方式。

请注意,朴素递归在这里是一个糟糕的计划。 如果你的图像有几十亿像素,这意味着第一次调用可能最终会有几十亿个递归调用。 这可能会让你的堆栈大吃一惊。

相反,维护自己的像素堆栈以进行访问,并通过在其中排队更多任务来递归。

struct location {
  int x,y;
};
bool visited_already(bool const*const* visit_flag, location l) {
  return visit_flag[l.y][l.x];
}
struct size {
  int x,y;
};
struct rectangle {
  location l;
  size s;
};
bool in_bounds( location l, rectangle b ) {
  if (l.x < b.l.x || l.y < b.l.y) return false;
  if (l.x >= b.l.x+b.s.x || l.y >= b.l.y+b.s.y) return false;
  return true;
}
bool do_visit(char*const* output, location l) {
  if (isUsefulPixel(l.x, l.y)) {
    output[l.y][l.x] = 1;
    return true;
  } else {
    return false;
  }
}
using todo_list = std::vector<location>;
bool extract_char( char*const* output, bool*const*visited, location where, rectangle bounds) {
  if (!in_bounds(where, bounds)) return false;
  if (visited_already(visited, where)) return false;
  visited[where.y][where.x] = 1;
  return do_visit(output, where);
}
void extract_chars(char*const* output, bool*const*visited, todo_list& list, rectangle bounds)
{
  while (!list.empty()) {
    auto next = list.back();
    list.pop_back();
    if (extract_char(output, visited, next, bounds))
    {
      list.push_back( {l.x+1, l.y-1} );
      list.push_back( {l.x+1, l.y+0} );
      list.push_back( {l.x+1, l.y+1} );
      list.push_back( {l.x+0, l.y-1} );
      list.push_back( {l.x+0, l.y+0} );
      list.push_back( {l.x+0, l.y+1} );
      list.push_back( {l.x-1, l.y-1} );
      list.push_back( {l.x-1, l.y+0} );
      list.push_back( {l.x-1, l.y+1} );
    }
  }
}
void do_start_extract_char(char *const*output, bool*const*visited, location where, rectangle bounds) {
  extract_chars( output, visited, {where}, bounds );
}