对于我的 Programming 102 课程,我们被要求提供可在 Linux 下编译和运行的 C 代码。我的硬盘上没有足够的空闲空间来安装 Linux 和 Windows,所以我使用 cygwin 来编译我的程序。
我必须提供的最新程序在 cygwin 下编译并运行良好。它在 Linux 下编译良好,但执行到一半会产生分段错误。我向给我们上课的研究生解释了这一点,他说 cygwin 的 GCC 版本允许编译和执行更草率的代码。
我通过谷歌找到的少数参考资料还没有定论。我发现的一个线程说Linux下seg错误的原因是内存泄漏。为什么这不会影响 cygwin 版本?
我会使用大学的计算机,但我不能在它们上使用 Subversion,这会严重阻碍我的努力。(我是编码新手,经常需要能够恢复到 X 之前的版本)。
cygwin 的 GCC 版本对它编译的代码真的更“宽松”吗?如果是这样,编码时是否有任何明显的问题需要注意?有没有其他方法可以编写在 Linux 下运行的代码?
编辑
感谢您的回复。我在原始帖子中不够明确:我的代码中有一个错误对我来说几乎是给定的(我对编程很陌生,毕竟在谈到 C 时真的很绿色)。我的 TA 暗示 cygwin 的 GCC 是一个不太可靠的编译器 - 允许运行更草率的代码 - 比在 GNU/Linux 下找到的编译器要低。我发现这很奇怪,所以在互联网上进行了搜索,但实际上找不到任何关于该事实的参考资料。
除了将编译器与我的代码归咎于我之外,我还想知道程序在 Windows 下运行并在 Linux 下崩溃的原因是什么。回复:Windows/Linux 下的不同内存管理器和堆/堆栈布局在这方面进行了说明。
cygwin 的 GCC 与 GNU/Linux 一样“好”的结论会不会非常正确?
关于发布源代码,这是一项家庭作业,所以如果可能的话,我宁愿自己找到问题:)
编辑 2
我已经接受了 jalf 的回答,因为它谈到了是什么让程序在 Windows 下而不是在 Linux 下运行,这是我真正想知道的。感谢所有做出贡献的其他人,他们都是非常有趣且内容丰富的回复。
当我发现问题并修复它时,我将上传一个包含这个非工作版本的所有源代码的 zip 文件,以防有人好奇我到底做了什么:)
编辑 3
对于那些有兴趣看代码的人,我发现了问题,并且确实是由于指针。我试图从函数返回一个指针。我试图返回的指针是在函数内部声明的,所以一旦函数执行就被销毁。有问题的代码在第 22-24 行被注释掉。
随意嘲笑我的代码。
/**
* Returns array of valid searches based on current coordinate
*/
void determine_searches(int row, int col, int last_row, int last_col, int *active_search){
// define coordinate categories and related valid search directions
int Library0[] = {2, 3, 4, -1};
int Library1[] = {4, 5, 6, -1};
int Library2[] = {2, 3, 4, 5, 6, -1};
int Library3[] = {0, 1, 2, 3, 4, 5, 6, 7, -1};
int Library4[] = {0, 1, 2, -1};
int Library5[] = {0, 6, 7, -1};
int Library6[] = {0, 1, 2, 6, 7, -1};
int Library7[] = {0, 1, 2, 3, 4, -1};
int Library8[] = {0, 4, 5, 6, 7, -1};
int * Library[] = {
Library0, Library1, Library2,
Library3, Library4, Library5,
Library6, Library7, Library8,
};
// declare (and assign memory to) the array of valid search directions that will be returned
//int *active_search;
//active_search = (int *) malloc(SEARCH_DIRECTIONS * sizeof(int));
// determine which is the correct array of search directions based on the current coordinate
// top left corner
int i = 0;
if(row == 0 && col == 0){
while(Library[0][i] != -1){
active_search[i] = Library[0][i];
i++;
}
}
// top right corner
else if(row == 0 && col == last_col){
while(Library[1][i] != -1){
active_search[i] = Library[1][i];
i++;
}
}
// non-edge columns of first row
else if(row == 0 && (col != 0 || col != last_col)){
while(Library[2][i] != -1){
active_search[i] = Library[2][i];
i++;
}
}
// non-edge coordinates (no edge columns nor rows)
else if(row != 0 && row != last_row && col != 0 && col != last_col){
while(Library[3][i] != -1){
active_search[i] = Library[3][i];
i++;
}
}
// bottom left corner
else if(row == last_row && col == 0){
while(Library[4][i] != -1){
active_search[i] = Library[4][i];
i++;
}
}
// bottom right corner
else if(row == last_row && col == last_col){
while(Library[5][i] != -1){
active_search[i] = Library[5][i];
i++;
}
}
// non-edge columns of last row
else if(row == last_row && (col != 0 || col != last_col)){
while(Library[6][i] != -1){
active_search[i] = Library[6][i];
i++;
}
}
// non-edge rows of first column
else if((row != 0 || row != last_row) && col == 0){
while(Library[7][i] != -1){
active_search[i] = Library[7][i];
i++;
}
}
// non-edge rows of last column
else if((row != 0 || row != last_row) && col == last_col){
while(Library[8][i] != -1){
active_search[i] = Library[8][i];
i++;
}
}
active_search[i] = -1;
}