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 Wind
Cygwin's version of gcc may have other default flags and tweaked settings (wchar_t being 2 bytes for example), but i doubt it is specifically more "lax" with code and even so - your code should not crash. If it does, then most probably there is a bug in your code that needs be fixed. For example your code may depend on a particular size of wchar_t or may execute code that's not guaranteed to work at all, like writing into string literals.
If you write clean code then it runs also on linux. I'm currently running firefox and the KDE desktop which together consist of millions of C++ lines, and i don't see those apps crashing :)
I recommend you to paste your code into your question, so we can look what is going wrong.
In the meantime, you can run your program in gdb
, which is a debugger for linux. You can also compile with all mudflap checks enabled and with all warnings enabled. mudflaps checks your code at runtime for various violations:
[js@HOST2 cpp]$ cat mudf.cpp
int main(void)
{
int a[10];
a[10] = 3; // oops, off by one.
return 0;
}
[js@HOST2 cpp]$ g++ -fmudflap -fstack-protector-all -lmudflap -Wall mudf.cpp
[js@HOST2 cpp]$ MUDFLAP_OPTIONS=-help ./a.out
... showing many options ...
[js@HOST2 cpp]$ ./a.out
*******
mudflap violation 1 (check/write): time=1234225118.232529 ptr=0xbf98af84 size=44
pc=0xb7f6026d location=`mudf.cpp:4:12 (main)'
/usr/lib/libmudflap.so.0(__mf_check+0x3d) [0xb7f6026d]
./a.out(main+0xb9) [0x804892d]
/usr/lib/libmudflap.so.0(__wrap_main+0x4f) [0xb7f5fa5f]
Nearby object 1: checked region begins 0B into and ends 4B after
mudflap object 0x9731f20: name=`mudf.cpp:3:11 (main) int a [10]'
bounds=[0xbf98af84,0xbf98afab] size=40 area=stack check=0r/3w liveness=3
alloc time=1234225118.232519 pc=0xb7f5f9fd
number of nearby objects: 1
*** stack smashing detected ***: ./a.out terminated
======= Backtrace: =========
....
There are many mudflap checks you can do, and the above runs a.out using the default options. Another tools which helps for those kind of bugs is valgrind
, which can also help you find leaks or off by one bugs like above. Setting the environment variable "MALLOC_CHECK_" to 1 will print messages for violations too. See the manpage of malloc
for other possible values for that variable.
For checking where your program crashes you can use gdb
:
[js@HOST2 cpp]$ cat test.cpp
int main() {
int *p = 0;
*p = 0;
}
[js@HOST2 cpp]$ g++ -g3 -Wall test.cpp
[js@HOST2 cpp]$ gdb ./a.out
...
(gdb) r
Starting program: /home/js/cpp/a.out
Program received signal SIGSEGV, Segmentation fault.
0x080483df in main () at test.cpp:3
3 *p = 0;
(gdb) bt
#0 0x080483df in main () at test.cpp:3
(gdb)
Compile your code with -g3 to include many debugging information, so gdb can help you find the precise lines where your program is crashing. All the above techniques are equally applicable for C and C++.