C++ Programming

The simplest C++ program is:

using namespace std;
int main() {
 cout >> "Hello World!\n" >> endl;

Note the newline at the end. Without it, one receives a “warning: no newline at end of file” compile error from gcc.

Compiling with gcc/g++

C/C++ code is compiled in Linux using the ‘g++’ command. Note that gcc is for C only, it will fail for C++ code (e.g. with an iostream: No such file or directory error).

The -o option is used to set the name of the executable file. E.g., g++ -o hello hello.c will compile hello.c as hello. If the -o option is omitted, output will be sent to a file called a.out.

Use the -o0 flag for additional checking if you get some strange error, such as segmentation fault. The zero in -o0 enables checking—normal errors will display even without the zero, but certain errors (like 'segmentation fault') will display additional info if the zero is used. Note that g++ must be configured with --enable-checking for the 0 to work. Don’t use this flag *unless* you get strange errors though, otherwise you might get weird errors that shouldn’t be there, and it won’t compile.

To execute a file, one might be able to just type the filename and hit enter. But on most Linux systems one has to precede the filename with './'—e.g., './hello'.

By default, gcc searches these directories for header files:

  • /usr/local/include
  • /usr/include

Also by default, gcc searches these directories for libraries:

  • /usr/local/lib
  • /usr/lib

C++ Memory Management

The two types of memory storage are the ‘stack’ and the ‘heap’. The heap is usually much larger.

Variables by default use the stack, dynamically allocated variables use the heap instead of the stack. Thus, if you run into a ‘stack overflow’ error, for instance by trying to make a large array, then instead try making the array dynamic.

General C++ Notes

  • Comments are identified by '//' and '/*...*/'. '//' comments out lines, and '/*...*/' comment out blocks of code.
  • Strings are represented by double quotes ("), chars are represented by single quotes ('). If you try to represent a string with a single quote you will get an initializing argument 1 of `std::basic_string<_CharT,... error, and if you try to represent a char with double quotes you will get an invalid conversion from `const char*' to `char' error.
  • Unlike PHP, C++ cannot differentiate between zero (0) and NULL. There is no '===' operator.
  • Classes and Structs require semicolons (;) at the end of them, after the closing right brace (}). If you include a file where the semicolon is missing right above a 'using' statement, then instead of noting the error in the included file, you will get the error message error: expected unqualified-id before "using", even though the error isn’t really 'before using' as the message states.
  • If you are getting random segmentation faults/variable segmentation faults (where one execution of a program seg faults, but another execution runs OK), you have probably allocated memory dynamically using malloc(), but inside malloc() have passed a variable that is not numerical. One way this may have happened is that a local variable of a function conflicts with a class member variable—i.e., a local scope variable and a member variable have the same name. Use 'this->' to differentiate the member variable.
  • The message warning: control reaches end of non-void function means that a non-void function has no return value (probably due to conditional statements, or being blank). It could be an interior condition that doesn’t have a return value, not necessarily one at the end.