General C++

Differences Between C and C++

Here are some differences I have come across between C and C++. This stuff is
interesting because it highlights the features that make C++ more powerful, and also
gives you a better understanding of the way the two languages work.

Biggest one: C does not have classes and objects

C++ has pass by reference. C does not.

C++ implements name mangling when compiling functions, C does not.

C Preprocessor (CPP)

Much of what I have learned comes from two places: the GNU CPP manual
link and a guide to the CPP
link.

__VA_ARGS__ is a CPP (c pre processor) identifier that gets replaced with the values
of … when used in a variadic macro.

A simple macro is a kind of abbreviation. It is a name which stands for a fragment of
code. Some people refer to these as manifest constants.

Before you can use a macro, you must define it explicitly with the
#define directive. #define is followed by the name of the macro
and then the code it should be an abbreviation for. For example,

#define BUFFER_SIZE 1020

The use of all upper case for macro names is a standard convention. Programs are easier
to read when it is possible to tell at a glance which names are macros.

The C preprocessor scans your program sequentially, so macro definitions take
effect at the place you write them.

foo = X;#define X 4
bar = X;

Produces as output:

foo = X;
bar =4;

You can also use #defines to define functions.

The do { } while(0) technique in macro definitions is used to avoid
issues with generating double expressions caused by the addition of a semicolon.
Doing the following:

#define SOME_MACRO(VAR) \
do { int i = var; \
i = 12*i; } \
while (0)

When you call this macro in the your code as SOME_MACRO(var); it expands into one
statement which is:

do{ ... }while(0);

This way you can use a macro expansion in a if else statement with no curly braces
where more than one statement would mess stuff up.

Rule of Three

Rule of Five

STL containers

The STL library has 3 types of container classes:

sequence containers - container classes that maintain the ordering of elements in the container. Examples are vectors, deques, and lists.

associative containers - container classes that automatically sort their inputs when the inputs are inserted into the container. Eg. are a set, multiset, map or multimap.

container adapters - special predefined containers that are adapted to specific uses, such as a LIFO stack or a FIFO queue.

STL containers provide a mechanism for traversing their contents called an iterator. An iterator is best visualized as a pointer to an element in the list with a set of overloaded operators that provide extra functionality.

Overloaded iterator operators:

Operator* dereferencing an iterator gives the element that the item is pointing to

Operator++ advances the iterator to point to the next element

Operator== & != gives basic comparison for two iterators to see if they point to the same element.

Operator= assign to a new location

Each container has functions that return an iterator.

begin() returns an iterator at the beginning of the container

end() returns an iterator at the start of the container

cbegin() returns a const iterator at the start of the container

cend() returns a const iterator at the end of the container

Iterators are members of an STL container. For example to get an iterator for a specific type of list:

std::map<int, std::string>::const_iterator mapIt = myMap.cbegin();

Vector

An std::vector is a sequence container dynamic array that can grow to add to more elements.

List

An std::list is a sequence container where each element contains a pointer to
the next and previous element. You can't randomly access elements, you have to
“walk the list”. But inserting elements is very fast if you know where to insert
them.

Set

An std::set is an ordered list of unique elements that get automatically
sorted as we insert them.

Multiset

An std::multiset is an ordered list of elements that can contain
duplicate data which is automatically sorted as we insert them.

Map

An std::map is an associated array which is a map, symbol table or
dictionary that has a collection of key value pairs such that a key only shows
up once.

Data pairs must be inserted into a map, and while you do this they are
automatically sorted. You can make pairs with the std::make_pair(x,y) helper
function.

Memory Leaks

For every new there must be a delete. Use the following valgrind command to
profile for memory leaks link.

valgrind--tool=memcheck --leak-check=yes name_of_exec

Lambda Functions

Lambda functions are:

functions without a name

define their functionality right in place

can be copied like data

Lambda functions should be precise and self explaining.

The syntax is as follows:

[] : captures the used variables per copy of per reference

() : is required for parameters

→ : is required for a return value

{} : may include expressions and statements

Range-based for statement

C++ 11 introduced the range-based for statement 2) and takes the form:

for ( for-range-declaration : expression ) statement

Example:

int array[5]={1,2,3,4,5};for(int& x : array)
x *=2;

Debugging

There are lots of options for debugging. I make extensive use of DDD with ROS
which helped a lot. gdbgui is an interesting web based front end, but i tried it
for 5 mins and ran into issues with it debugging a simple program.

GDB

Debugging in GDB is not that bad! It is also really useful when looking at simple
stuff for learning stuff.