有人可以解释我为什么会出现分段错误吗?

时间:2015-11-28 01:45:57

标签: c segmentation-fault

所以我有一个项目正在解决分段错误,我不知道为什么。该项目旨在创建一个2D矩阵字拼图并根据输入搜索单词。我可以轻松找到从左到右,从右到左的单词,但是当我添加自上到下和从下到上的函数时,无论如何都会出现分段错误!我将附上我的代码,以便人们可以看到问题所在:

//Checking for the first letter before going to sub categories
void checkForWord(char *str, char **puzzle, int rows, int columns) {
        int r,c,i=0;

         for(r = 0; r < rows; r++) {
          for(c = 0; c < columns; c++){
            if(puzzle[r][c] == str[i]) {
                if(c != columns) {
                    if(puzzle[r][c+1] == str[i+1]) {
                        if(restOfWordLR(str, puzzle, r, c+1, 2) == 1) {
                            lrCount++;
                            totalCount++;
                            printf("TesT\n");
                        }
                    }
                }
                if(c != 0) {
                    if(puzzle[r][c-1] == str[i+1]) {
                        if(restOfWordRL(str, puzzle, r, c-1, 2) == 1) {
                            rlCount++;
                            totalCount++;
                        }
                    }
                }
                if(r != rows) {
                    if(puzzle[r+1][c] == str[i+1]) {
                        if(restOfWordTB(str, puzzle, r+1, c, 2) == 1) {
                            tbCount++;
                            totalCount++;
                        }
                    }
                }
                if(r != 0) {
                    if(puzzle[r-1][c] == str[i+1]) {
                        if(restOfWordBT(str, puzzle, r-1, c, 2) == 1) {
                            btCount++;
                            totalCount++;
                        }
                    }
                }
           }
       }
    }
}

1 个答案:

答案 0 :(得分:2)

if(c != columns) {
     if(puzzle[r][c+1] == str[i+1]) {

c == columns-1 c != columnsc+1 == columnsif(r != rows)仍然不在边界时,仍可访问数组边界。

同样的问题也发生在if (c < columns-1)代码块上。

您可以将其更改为public class ReportGenarator { public static String OUT_PUT = "your_output_file_path/myreport.docx"; public static String REPORT = "your_report_path/myreport.jrxml"; public void genarateReport(String reportPath, Map<String, Object> map, Connection con) { try { JasperReport jr = JasperCompileManager.compileReport( ClassLoader.getSystemResourceAsStream(reportPath)); JasperPrint jp = JasperFillManager.fillReport(jr, map, con); JRDocxExporter export = new JRDocxExporter(); export.setExporterInput(new SimpleExporterInput(jp)); export.setExporterOutput(new SimpleOutputStreamExporterOutput(new File(OUT_PUT))); SimpleDocxReportConfiguration config = new SimpleDocxReportConfiguration(); export.setConfiguration(config); export.exportReport(); } catch (JRException ex) { ex.printStackTrace(); } } } 。确保在修改后检查逻辑的正确性。