Contents
- Prevent your program from going crazy if a user enters a letter instead of a number
- Easy compilers to use on Windows and Linux
- Enable execution of Linux programs on a Windows vfat partition
C++ Programming Tips
Prevent your program from going crazy if a user enters a letter instead of a number
Modified: 2006-09-23 09:25:10
See the last code example on C++ Tutorial | 6. Common Student Problems | Keyboard Input.
Easy compilers to use on Windows and Linux
Modified: 2006-09-23 11:03:28
On Windows use Dev-C++.
To compile on Linux just run:
g++ your_file.cpp
then you need to make the program executable:
chmod +x a.out
Then run it:
./a.out
If you're using a line like this in your program:
system("Pause");
you will get an error like this on the console when your program reaches that point:
ssh: Pause: command not found
But the main reason for calling that is so that on Windows the program window does not dissapear when the program finishes. On Linux that is not needed, so if you have that at the end of your program, don't worry about it.
Enable execution of Linux programs on a Windows vfat partition
Modified: 2006-09-23 11:50:57
Open /etc/fstab and find the line like this
/dev/sda2 /media/shared vfat users,rw,umask=000 0 0
Add the exec option so it looks like this:
/dev/sda2 /media/shared vfat users,rw,umask=000,exec 0 0
There, now you can share your Linux files with with Windows and execute your C++ programs also.
Debugging Tips
Using GDB to find the line number causing a Segmentation fault
Modified: 2008-02-05 20:21:16
Note, to debug a C++ program, just substitute g++ for gcc in the example below:$ cat segfault.cNow debugging Segmentation faults in C programs just got a little bit easier. :)
int main() {
int* ptr = (int*) 0x00000000;
*ptr = 1;
}
$ gcc -g segfault.c
$ ./a.out
Segmentation fault (core dumped)
$ gdb ./a.out
GNU gdb 6.6-debian
...
This GDB was configured as "i486-linux-gnu"...
Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".
(gdb) run
Starting program: /home/elijahlofgren/tmp/c/a.out
Program received signal SIGSEGV, Segmentation fault.
0x0804835f in main () at segfault.c:3
3 *ptr = 1;
Now, sometimes, the segmentation fault occurs outside your code so you need to run "bt" using gdb to see the backtrace. Here is an example:
elijahlofgren@kubuntu:~/tmp/c$ cat gdb-backtrace.c
#include<stdio.h>
int main() {
int i;
for (i = 0; i < 10; i++) {
printf("i (should use d instead of s for int): %s\n", i);
}
}
elijahlofgren@kubuntu:~/tmp/c$ gcc -g gdb-backtrace.c
elijahlofgren@kubuntu:~/tmp/c$ ./a.out
i (should use d instead of s for int): (null)
Segmentation fault (core dumped)
elijahlofgren@kubuntu:~/tmp/c$ gdb ./a.out
GNU gdb 6.6-debian
... This GDB was configured as "i486-linux-gnu"...
Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".
(gdb) run
Starting program: /home/elijahlofgren/tmp/c/a.out
i (should use d instead of s for int): (null)
Program received signal SIGSEGV, Segmentation fault.
0xb7e99bfb in strlen () from /lib/tls/i686/cmov/libc.so.6
(gdb) bt
#0 0xb7e99bfb in strlen () from /lib/tls/i686/cmov/libc.so.6
#1 0xb7e6c0cb in vfprintf () from /lib/tls/i686/cmov/libc.so.6
#2 0xb7e71653 in printf () from /lib/tls/i686/cmov/libc.so.6
#3 0x080483a1 in main () at gdb-backtrace.c:5
(gdb)
Have gcc warn you of unused variables
Modified: 2008-04-20 20:16:57
Call gcc like this:
gcc your_file.cpp -Wunused