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