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.