分段错误,没有详细的编译警告
Segmentation fault with no detailed compiling warnings
我正在努力解决一个难题,有一段时间进展顺利。我习惯于遇到分割错误,但我已经克服了它们,直到这次。因为在这篇文章中,我没有得到任何指示,指出分段错误可能发生在哪里。我使用了Dev C++和MS Visual studio Community 2015,但他们都没有向我提供任何关于这一行的信息,这是错误的来源。
我正在分享下面的整个代码,然后我将分享我认为问题来源最重要的特定功能块:
#include <iostream>
#include <vector>
int boardSize, result=0;
int * board;
std::vector<std::vector<int> > tempVec;
void askSize()
{
//
std::cout << "type in the size of the boardSize: "<< std::endl;
std::cin >> boardSize;
std::cout << std::endl;
board = new int [boardSize];
for(int i = 0; i < boardSize; i++)
{
//
if( i < boardSize/2 )
{
//
//std::cout<< i;
board[i] = 1;
}
else
{
board[i] = 0;
}
}
for( int i = 0; i < boardSize; i++ )
{
//
std::cout << *(board + i) <<std::endl;
}
std::cout << std::endl;
}
void showBoard()
{
//
for( int i = 0; i < boardSize; i++ )
{
//
std::cout << *(board + i) << ", " << std::endl;
}
std::cout << std::endl;
}
int git(int * arrX, int x)
{
//
if( arrX[x+1] == 0 )
{
//
arrX[x] = 0;
arrX[x+1] = 1;
return 1;
}
else
{
return 0;
}
}
int checkGit(int * arrX, int x)
{
//
if( arrX[x+1] == 0 )
{
return 1;
}
else
{
return 0;
}
}
void atla(int * arrX, int y, int jumpCount)
{
//
if( ( jumpCount > 0 && arrX[y+jumpCount*2+2] == 1 && y < boardSize-2 ) || ( jumpCount > 0 && arrX[y+jumpCount*2+1] == 0 && y < boardSize-2 ) )
{
//
arrX[y] = 0;
arrX[y + jumpCount*2] = 1;
//return jumpCount;
}
else if( jumpCount > 0 && arrX[y+jumpCount*2+2] == 0 && y < boardSize-2 && arrX[y+jumpCount*2+1] == 1)
{
//
atla(arrX, y, jumpCount+1);
}
else if( jumpCount == 0 && arrX[y+2] == 0 && arrX[y+1] == 1)
{
//
atla(arrX, y, jumpCount+1);
}
else if( ( jumpCount == 0 && arrX[y+2] == 1 ) || ( jumpCount == 0 && arrX[y+1] == 0 ) )
{
//
//return 0;
}
}
int checkBoard(int * arrX)
{
//
for( int i = 0; i < boardSize; i++ )
{
//
if( i < boardSize/2 )
{
//
if( arrX[i] == 1 )
{
//
return 0;
}
}
else
{
//
if( arrX[i] == 0 )
{
//
return 0;
}
}
}
return 1;
}
int checkAtla( int * arrX, int x )
{
//
if( arrX[x + 1] == 1 && arrX[x + 2] == 0 )
{
//
return 1;
}
else
{
//
return 0;
}
}
void changeTempArr(int x, int y, int * arrX)
{
//
if( y == 0 )
{
//
git(arrX, x);
}
else if( y == 1 )
{
//
atla(arrX, x, 0);
}
}
void startBusiness( int counter, int * arrX )
{
//
/*
int * tempArr = new int [boardSize];
for( int i = 0; i < boardSize; i++ )
{
//
tempArr[i] = arrX[i];
}
*/
for( int i = 0; i < boardSize; i++ )
{
//
if( checkAtla(arrX, i) == 1 && i < boardSize - 2 )
{
//
tempVec.push_back({i,1});
}
if( checkGit(arrX, i) == 1 && i < boardSize - 1 )
{
//
tempVec.push_back({i,0});
}
}
if( tempVec.size() > 0 )
{
//
int ** possibilities = new int * [tempVec.size()];
for( int i = 0; i < tempVec.size(); i++ )
{
//
possibilities[i] = new int[2];
possibilities[i][0] = tempVec[i][0];
possibilities[i][1] = tempVec[i][1];
}
int possibilitySize = tempVec.size();
tempVec.clear();
//std::cout<< possibilities[10][0] << ", " << possibilities[10][1];
for( int i = 0; i < possibilitySize; i++ )
{
//
int * tempArr = new int [boardSize];
for( int i = 0; i < boardSize; i++ )
{
//
tempArr[i] = arrX[i];
//std::cout << tempArr[i] << std::endl;
}
changeTempArr(possibilities[i][0], possibilities[i][1], tempArr);
//std::cout << "check" << std::endl;
startBusiness( counter + 1, tempArr );
}
}
else
{
//
if( checkBoard(arrX) == 1 )
{
//
if( counter > result)
{
//
result = counter;
std::cout << result << std::endl;
}
}
}
}
int main()
{
askSize();
startBusiness(0,board);
std::cout << "the result is: " << result;
return 0;
}
我认为问题在这两行之间(在启动业务功能中):
int ** possibilities = new int * [tempVec.size()];
for( int i = 0; i < tempVec.size(); i++ )
{
//
possibilities[i] = new int[2];
possibilities[i][0] = tempVec[i][0];
possibilities[i][1] = tempVec[i][1];
}
int possibilitySize = tempVec.size();
tempVec.clear();
//std::cout<< possibilities[10][0] << ", " << possibilities[10][1];
for( int i = 0; i < possibilitySize; i++ )
{
//
int * tempArr = new int [boardSize];
for( int i = 0; i < boardSize; i++ )
{
//
tempArr[i] = arrX[i];
//std::cout << tempArr[i] << std::endl;
}
changeTempArr(possibilities[i][0], possibilities[i][1], tempArr);
//std::cout << "check" << std::endl;
startBusiness( counter + 1, tempArr );
}
因为我第一次可以输出本地"I"=0的"tempArr[I]",然后事情变得一团糟。但真的看不出哪一条线是有问题的。还有另一个问题,这可能是与内存有限有关的问题吗?
谢谢。
在startBusiness
函数中,您有
if( checkAtla(arrX, i) == 1 && i < boardSize - 2 )
{
//
tempVec.push_back({i,1});
}
这将首先调用checkAtla
,然后检查范围。因此,您使用i==boardSize-2
调用checkAtla
,将导致segfault。
相关文章:
- 关于std::move的使用,是否有编译警告
- 示例C++项目编译中的警告
- Steam 回调在 Linux / GCC 编译期间触发警告
- C++警告:编译指示在主文件中一次
- 如何在类成员函数中获得类似printf中的编译警告
- 编译按位 OR 时出现警告 C4805
- 窄转换编译警告.如何解决它
- C++就地析构函数编译警告
- 如何在 ubuntu 12.10 中隐藏 g++ c++11 编译警告
- 在Ubuntu中使用OpenCV进行编译警告
- 收到有关压缩结构的意外编译警告
- 在OpenCV中编译警告,并且没有输出
- 每个g++的minGW编译警告
- 在linux服务器(u2)上编译警告long-long
- 由于函数指针类型定义与具有正确const逻辑的指定函数不匹配,导致编译警告
- 分段错误,没有详细的编译警告
- 编译警告不存在(GCC 和 g++)
- c++ 11类型检查代码的奇怪编译警告
- 包含不可复制对象的对象的编译警告是不可读的
- 长类型Max等于int Max error + math.h pow()编译警告:隐式常量转换溢出