Memory leaks in constructions

Say that you have found somewhere deep in the darkest basement the following line of code

then you have found a potential memory leak in your program.


Remember that, calling new Date() will:

  1. Allocate memory for Date
  2. Construct the new Date object in that memory. If the construction fails because of an exception, then the allocated memory is freed again.

The compiler is free to evaluate the expressions in any way it seems suitable (the reason for this is that it has the ability to optimize the code if given some slack, see order of evaluation).  It can for example do like this:

  1. Allocate memory for Date
  2. Construct Date
  3. Allocate memory for Time
  4. Construct Time
  5. Call fun

The problem here is that if either step 3 or step 4 fails because of an exception, the C++ standard does not require that the Date object must be destroyed and its memory deallocated. This is a memory leak!

Another possibility is that the compiler does this:

  1. Allocate memory for Date
  2. Allocate memory for Time
  3. Construct Date
  4. Construct Time
  5. Call fun

In this case, then there are actually two problems.

1) If step 3 fails because of an exception, then the memory allocated for Date is automatically deallocated (step 1 is undone), but the standard does not require that the memory allocated for the Time object be deallocated. The memory is for the Time object is leaked.

2) If step 4 fails because of an exception, then the Date object has been allocated and fully constructed, but the standard does not require that it be destroyed and its memory deallocated. The memory of the Date object is leaked.






Order of evaluation

In standard C++ (and C) must

  1. All of a functions arguments be full evaluated before the function is called.
  2. No function calls may interleave with one another, a prior function call must be fully done before the next can start.
  3. Expressions used as function arguments can be evaluated in any order, also interleaved(!) unless this is prohibited by rule 1 or 2.

This means that in the expression

then expression1 and expression2 must be evaluated before func is called, but we don’t know which of them is evaluated first.

In the more complicated example of

then we know that

  1. expression1 is evaluated before g
  2. expression2 is evaluated before h
  3. both g() and h() are evaluated before f() is called.

However, we don’t know if

  • expression1 or expression2 is evaluated first
  • g() or h() is evaluated first
  • expression1 is fully evaluated before h() is called (!)

The const keyword

The const keyword is used to show both other developers and the compiler that an object is constant and its contents cannot be changed. If you anyway try to change the contents of a const object will generate an error already at compile time. It is a good practice to add the const keyword to a parameter, a members or a method to show that the value is not going to be changed. This can both make the intent of the code more clear to the user, and can make the code faster since the compiler can make certain optimizations knowing that the object is not going to change its value.

The difference between the const keyword and the constexpr keyword is that the value of a constexpr constant must be resolved at compile time whereas the value of a const constant can be resolved at run time (or at compile time).

The const keyword can be used in the following contexts:

Const class

An object can be made constant at creation by declaring the object as const:

Any attempt to change the contents of the object christmasDay will result in a compiler error:


Const member functions

Adding the const keyword to a member function (added at the end of the function declaration, after the parameters) states that this method will not change the state of the object.

The const keyword after the declaration of the method getYear() shows that calling this method will not change any members of the object. This also means that we are allowed to call this method even if the object is const.


Const parameters

Parameters to functions can also be declared as const. There are two positions where the const keyword can be added.





Converting std::vector from a C array and vice versa

Sometimes it is necessary to interact with C code from a C++ program. We would then need to extract a pointer out from a vector, or vice-versa, create a vector from a pointer to data without having to worry about the extra memory management this may cause.

This one line of code will convert a pointer to an array and the number of elements in the array and into a vector from the C++ standard library:

This creates a copy of the data, so that when the local vector goes out of scope then the input pointer will not be deleted.

To get a C style array out from a C++ vector, use:

calling the method data() returns a pointer to the first element in the vector. This pointer must never be deleted, the vector will take care of cleaning up its contents.