C++ Programming

C++ Programming


How to find bugs

Posted: 26 Apr 2013 09:52 AM PDT

Print information during execution The best way to find errors is to print a lot of information about the internal state of the program. For instance, if a program remains frozen, the first thing to do is to print something when a few checkpoints are met cout << "Checkpoint #1\n"; for(int i = 1; i < 1000; i++) cout << "i = " << i << "\n"; cout << "Checkpoint #2\n"; for(int j = 0; j < 100; j =

How to avoid bugs

Posted: 26 Apr 2013 09:28 AM PDT

Write in parts Finding one bug in the 20 lines you typed for the last fifteen minutes is easier than finding fifty bugs in the two thousands lines you have typed for one month. Good identifiers Use long identifiers such as sum_of_the_weights instead of short ones. Use longer identifiers for variables and functions used in many parts of the program. If a variable is used only in a 5

The Bug Zoo

Posted: 26 Apr 2013 09:06 AM PDT

The program crashes: Segmentation fault This family of problem is extremely large and contains two main sort of errors: access to non-authorized part of the memory and system calls with incorrect parameter values. Unauthorized memory access It when you try to read or write to a memory address 1. totally meaningless (for instance a non-initialized pointer) 2. out of bounds

Declaration Vs definition

Posted: 26 Apr 2013 08:43 AM PDT

Declaration Vs definition The declaration of a function specifies the return and parameter types. The definition specifies the part of the program associated to the function, i.e. the statement between { }. Up to now, we have always done the declaration and the definition simultaneously, but declaration can be done before the definition. This is useful because a function can be called at

Testing Null Pointer

Posted: 26 Apr 2013 08:37 AM PDT

Pointer arthmatic  A pointer can be implicitly converted into a bool. All non-null pointers are equivalent to true and the null one is false. The convention is that the null pointer correspond to a non-existing object. Static pointers are not initialized to 0 when the program starts, so you have to be very careful when using this convention For example, if we want to write a function that

Dynamic allocation

Posted: 26 Apr 2013 08:28 AM PDT

Dynamic allocation Why And How  In many situations the programmer does not know when he writes a program what objects he will need. It can be that he does not know if he will need a given object, or or that he does not know the size of a required array. The new operator allows to create an object at run-time. This operator takes as an operand a type, which may be followed either by a

What are Built-in arrays

Posted: 26 Apr 2013 07:58 AM PDT

What are Built-in arrays What are Built-in arrays The "" operator defines arrays of char. Similarly, we can define an array of any type with the [ ] operator : int n[4] = { 1, 2, 3, 4 }; The compiler is able to determine by itself the size of an array, so you do not have to specify it : int poweroftwo[] = { 1, 2, 4, 8, 16, 32, 64, 128 }; As we said, the compiler keeps the

Understanding Functions in C++

Posted: 26 Apr 2013 07:51 AM PDT

Understanding Functions in C++ To re-use the same part of a program, we can define a function, which takes parameters, and returns a result of various type. Typical definition contains theType of the value it returns, an identifier for its name, and the list of parameters with their types. The evaluation of a function is done when the call operator () is used. One argument (i.e. an

The abort() function in C++

Posted: 26 Apr 2013 04:28 AM PDT

The abort() function is wich interrupt the execution of your program as if there was a serious error. Use it to handle non-expected behavior like out-of bounds exceptions : #include <iostream> int main(int argc, char **argv) { int x; cout << "Enter a non-null value : "; cin >> x; if(x == 0) { cerr << "Null value!\n"; abort(); } cout << 1/x << '\n'; } The execution is the following : Enter a

The sizeof operator

Posted: 26 Apr 2013 04:07 AM PDT

We can know the memory usage of a given type using the sizeof operator. It takes either a variable name or a type name as parameter. #include <iostream> int main(int argc, char **argv) { int n; cout << sizeof(n) << ' ' << sizeof(double) << '\n'; } The result is 4 8.

Streams, include files

Posted: 26 Apr 2013 04:03 AM PDT

Beside the built-in types, the C++ compiler is often accompanied with lot of files containing predefined types. The main one we will use in our example programs is the stream type. To use it, we need to indicate to the compiler to include in our source file another file called iostream (where this class type is defined). #include <iostream> int main(int argc, char **argv) { int k; k = 14; k


Post a Comment