与Cygwin的Windows中编译C程序的工作原理,段故障Linux下。是Cygwin的GCC'坏'? [英] C program compiled with cygwin in Windows works, segmentation fault under Linux. Is cygwin GCC 'bad'?

查看:211
本文介绍了与Cygwin的Windows中编译C程序的工作原理,段故障Linux下。是Cygwin的GCC'坏'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我的编程102类,我们被要求提供C code,它编译和Linux下运行。我没有我的硬盘上有足够的空闲空间来安装Linux的沿着窗户,所以我用cygwin来编译我的程序。

从最近我在编译给予和在cygwin下运行良好。它在Linux下编译好,但通过执行中途产生分段错误。我解释了这对毕业的学生谁给了我们班,他说,cygwin的GCC的版本允许更模糊code进行编译和执行。

我已经通过谷歌找到的几个引用还没定论。一个线程我发现说,Linux下赛格故障原因是内存泄漏。为什么会这样不会影响Cygwin的版本?

我将使用该大学的计算机,但我不能使用Subversion他们这会阻碍显著我的努力。 (我是新的编码和经常需要能够能够前还原于X修订)。

时Cygwin的GCC的版本确实更多的'宽松'与code它编译?如果是这样,是否有任何明显的问题看出来编码时?是否有能够编写code将在Linux下运行任何替代方案?

修改

谢谢你的答复。我是不是在我原来的职位不够明确:有我在code的错误是pretty太大的,我给(我很新的节目,真正的绿色,当谈到到C后,所有)。我的助教暗示Cygwin的海湾合作​​委员会是一个不可靠的编译器 - 允许大部分更模糊code比GNU / Linux下发现一对运行。我发现这个奇怪的,因此有互联网上搜索,但不能真正找到了这一事实的任何引用。

不是埋怨编译器对我的code的,我想知道的原因可能是什么程序在Windows下运行,并坠毁在Linux下。答复重:不同的内存管理和堆/在Windows / Linux软件栈布局在这方面进行了说明。

请问这样的结论Cygwin的GCC是一样'好'作为GNU / Linux的,它的底层操作系统/侥幸,我的车程序的一个下运行,而不是其他是pretty多少是否正确?

关于发布源$ C ​​$ C,这是一个家庭作业,所以我倒是preFER找到自己的问题,如果在所有可能的:)

编辑2

我已经接受jalf的答案,因为它谈论什么使Windows下,而不是在Linux下运行的程序,这是我真正想知道的。感谢所有的人谁作出了贡献,他们都非常有趣和翔实的答复。

当我发现这个问题,并固定它,我会上传一个zip文件与本次非工作版本的所有源$ C ​​$ C,如果任何人都好奇,想看看到底我所做的:)

修改3

对于那些有兴趣看到code,我发现这个问题,这确实是由于指针。我试图从一个函数返回一个指针。我试图返回指针已被宣布里面的功能,因此被被破坏,一旦功能执行。问题code被注释掉上线22-24。

随意嘲笑我的code。

  / **
根据目前的有效搜索*回报阵列坐标
* /
无效determine_searches(INT行,诠释山口,诠释LAST_ROW,诠释last_col,为int * active_search){
    //定义协调类别和相关的有效的搜索方向
    INT Library0 [] = {2,3,4,-1};
    INT分享帮助[] = {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 *库[] = {
        Library0,分享帮助,Library2,
        Library3,Library4,Library5,
        Library6,Library7,Library8,
    };    //声明(和分配存储器给)有效的搜索方向的阵列将被返回
    //为int * active_search;
    // active_search =(INT *)malloc的(SEARCH_DIRECTIONS *的sizeof(INT));
    //确定这是基于当前搜索方向的正确坐标数组
    //左上角
    INT I = 0;
    如果(行== 0安培;&安培;西== 0){
    而(图书馆[0] [I]!= -1){
    active_search [I] =库[0] [I];
    我++;
    }
    }
    //右上角
    否则,如果(行== 0安培;&安培;山坳== last_col){
    而(图书馆[1] [I]!= -1){
    active_search [I] =库[1] [I];
    我++;
    }
    }
    //第一行的非边缘列
    否则,如果(行== 0安培;&安培;!(COL = 0 ||山坳= last_col)){
    而(库[2] [I]!= -1){
    active_search [I] =库[2] [I];
    我++;
    }
    }
    //非边缘坐标(无边沿列,也不行)
    否则,如果(行= 0&安培;!&安培;排= LAST_ROW和放大器;!&安培;西= 0&安培;!&安培;!山坳= last_col){
    而(库[3] [I]!= -1){
    active_search [I] =库[3] [I];
    我++;
    }
    }
    //左下角
    否则,如果(行== LAST_ROW&放大器;&安培;西== 0){
    而(库[4] [I]!= -1){
    active_search [I] =库[4] [I];
    我++;
    }
    }
    //右下角
    否则,如果(行== LAST_ROW&放大器;&安培;山坳== last_col){
    而(库[5] [I]!= -1){
    active_search [I] =库[5] [I];
    我++;
    }
    }
    //最后一行的非边缘列
    否则,如果(行== LAST_ROW&放大器;&安培;!(COL = 0 ||山坳= last_col)){
    而(图书馆[6]由[i]!= -1){
    active_search [I] =库[6]由[i];
    我++;
    }
    }
    //第一列的非边缘行
    否则,如果((行= 0 ||排= LAST_ROW)及!&放大器;西== 0){
    而(图书馆[7]由[i]!= -1){
    active_search [I] =库[7]由[i];
    我++;
    }
    }
    //最后一列的非边缘行
    否则,如果((行= 0 ||排= LAST_ROW)及!&放大器;山坳== last_col){
    而(图书馆[8]由[i]!= -1){
    active_search [I] =库[8]由[i];
    我++;
    }
    }
    active_search [I] = -1;
}


解决方案

像其他人所说的,你可能想张贴一些您的code在这里,即使那不是你的问题的真正的重点。它可能仍然是一个很好的学习经验,在这里大家通过你的code捅,看看他们是否能找到什么引起的段错误。

但是,是的,问题是,有一个影响C程序这么多的依赖于平台的,以及基本上随机因素。虚拟内存是指的有时的,访问未分配的内存将似乎工作,因为你打的是在一些早期的点被分配一个页面的未使用的部分。其他时候,它会出现段错误,因为你打一个尚未分配给你的进程在所有的页面。这真的是不可能predict。这取决于您的分配内存,它是在页面的边缘,或在中间?这是最高的网页迄今已分配的操作系统和内存管理器,并和......你的想法。不同的编译器,不同版本的同一编译器,不同OS'es,不同的软件,驱动程序或系统中安装的硬件,什么都可以改变你是否没有得到段错误当您访问未分配的内存。

对于TA声称cygwin的是更多的宽松,这是垃圾,一个简单的原因。
无论是编译器捕获的错误!如果本地的GCC编译器已经真正已经不太宽松,它会给你在编译时错误。段错误不是由编译器产生。没有太多的编译器可以做,以确保你得到的而不是表面上工作的程序段错误。

For my Programming 102 class we are asked to deliver C code that compiles and runs under Linux. I don't have enough spare space on my hard drive to install Linux alongside Windows, and so I use cygwin to compile my programs.

The most recent program I had to give in compiles and runs fine under cygwin. It compiles fine under Linux, but half-way through execution produces a segmentation fault. I explained this to the grad student who gives us class and he said that cygwin's version of GCC allows for sloppier code to be compiled and executed.

The few references I have found via google haven't been conclusive. One thread I found said that the cause for the seg fault under Linux is a memory leak. Why would this not affect the cygwin version?

I would use the University's computers, but I can't use Subversion on them which would significantly hinder my efforts. (I'm new to coding and often need to be able to be able to revert to X revisions ago).

Is cygwin's version of GCC really more 'lax' with the code it compiles? If so, are there any obvious issues to look out for when coding? Are there any alternatives for being able to write code that will run under Linux?

Edit

Thanks for the replies. I wasn't explicit enough in my original post: that there is a bug in my code was pretty much a given for me (I am quite new to programming, and really green when it comes to C, after all). My TA implied cygwin's GCC is a less reliable compiler -allowing for much sloppier code to run- than the one found under GNU/Linux. I found this strange and so had a search on the internet, but couldn't really find any references to that fact.

More than blaming the compiler vs. my code, I was wondering what the reason could be for the program to run under Windows and crash under Linux. The replies re: different memory managers and heap/stack layout under Windows/Linux were illustrating in that regard.

Would the conclusion that cygwin's GCC is just as 'good' as GNU/Linux', and it's the underlying operating systems/sheer luck that my buggy program runs under one and not the other be pretty much correct?

Regarding posting the source code, it's a homework assignment so I'd prefer to find the issue myself if at all possible :)

Edit 2

I've accepted jalf's answer as it talks about what makes the program run under Windows and not under Linux, which was what I really wanted to know. Thanks to everyone else who contributed, they were all very interesting and informative replies.

When I've found the issue and fixed it I'll upload a zip file with all the source code of this non-working version, in case anyone is curious to see what the hell I did :)

Edit 3

For those interested in seeing the code, I found the problem, and it was indeed due to pointers. I was trying to return a pointer from a function. The pointer I was trying to return was being declared inside the function and so was being destroyed once the function executed. Problematic code is commented out on lines 22-24.

Feel free to ridicule my code.

/**
*  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;
}

解决方案

Like others have said, you might want to post some of your code here, even if that's not the real point of your question. It might still be a good learning experience to have everyone here poke through your code and see if they can find what caused the segfault.

But yeah, the problem is that there are so many platform-dependent, as well as basically random, factors influencing a C program. Virtual memory means that sometimes, accessing unallocated memory will seem to work, because you hit an unused part of a page that's been allocated at some earlier point. Other times, it'll segfault because you hit a page that hasn't been allocated to your process at all. And that is really impossible to predict. It depends on where your memory was allocated, was it at the edge of a page, or in the middle? That's up to the OS and the memory manager, and which pages have been allocated so far, and...... You get the idea. Different compilers, different versions of the same compilers, different OS'es, different software, drivers or hardware installed on the system, anything can change whether or not you get a segfault when you access unallocated memory.

As for the TA's claim that cygwin is more "lax", that's rubbish, for one simple reason. Neither compiler caught the bug! If the "native" GCC compiler had truly been less lax, it would have given you an error at compile-time. Segfaults are not generated by the compiler. There's not much the compiler can do to ensure you get a segfault instead of a program that seemingly works.

这篇关于与Cygwin的Windows中编译C程序的工作原理,段故障Linux下。是Cygwin的GCC'坏'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆