C/C++: The Hazards of Using a Minimal Amount of Code

This first article in a series devoted to how C/C++ programmers play with fire without knowing it, will discuss how programmers often attempt to explicitly call a constructor.

Programmers are lazy creatures. That’s why they tend to solve a task using a minimal amount of code. There’s nothing wrong with this approach, in fact, it’s good but it’s important to not get too involved in the process and to stop at the right time.

For example, programmers are too lazy to create a single initialization function in a class so that it can be called from various constructors later. They think: “Why do I need an extra function? I’d rather call one constructor from the other”. Unfortunately, sometimes programmers can’t solve even this simple of a task. It is to detect such unsuccessful attempts that I’m implementing a new rule in PVS-Studio. Here, for instance, is a code sample I have found in the eMule project:

class CSlideBarGroup
{
public:
  CSlideBarGroup(CString strName,
    INT iIconIndex, CListBoxST* pListBox);
  CSlideBarGroup(CSlideBarGroup& Group);
  ...
}

CSlideBarGroup::CSlideBarGroup(CSlideBarGroup& Group)
{
  CSlideBarGroup(
    Group.GetName(), Group.GetIconIndex(), Group.GetListBox());
}

Let’s examine more attentively how the last constructor is implemented. The programmer decided that the following code…

CSlideBarGroup(
  Group.GetName(), Group.GetIconIndex(), Group.GetListBox());

…simply calls the other constructor. Nothing of the kind. A new unnamed object of the CslideBarGroup type is created and destroyed right after.

It appears that the programmer has actually called the other constructor. But he/she has not quite done what he/she intended: the class fields remain uninitialized.

Such errors are only half the trouble. Some people do know how to call the other constructor, and they do it. I wish they didn’t know how.

For instance, the above given code could be rewritten in this way:

CSlideBarGroup::CSlideBarGroup(CSlideBarGroup& Group)
{
  this->CSlideBarGroup::CSlideBarGroup(
    Group.GetName(), Group.GetIconIndex(), Group.GetListBox());
}

…or in this way:

CSlideBarGroup::CSlideBarGroup(CSlideBarGroup& Group)
{
  new (this) CSlideBarGroup(
    Group.GetName(), Group.GetIconIndex(),
    Group.GetListBox());
}

Now one data initialization constructor is really calling the other constructor.

If you see a programmer doing this, deal him/her one flick on his/her forehead for yourself and one more flick on my behalf.

The cited examples contain very dangerous code and you should understand well how they work!

Being written for the purpose of petty optimization (programmers are too lazy to write a separate function), this code might do more harm than good. Let’s look more closely as to why such constructs sometimes work but most often don’t.

class SomeClass
{
  int x,y;
public:
  SomeClass() { new (this) SomeClass(0,0); }
  SomeClass(int xx, int yy) : x(xx), y(yy) {}
};

This code will work correctly. It is safe and works well, since the class contains primary data types and is not a descendant of other classes. In this case, a double constructor call is harmless.

Let’s consider another code where an explicit constructor call causes an error (the sample is taken from the discussion on the StackOverflow website):

class Base
{
public:
 char *ptr;
 std::vector vect;
 Base() { ptr = new char[1000]; }
 ~Base() { delete [] ptr; }
};

class Derived : Base
{
  Derived(Foo foo) { }
  Derived(Bar bar) {
     new (this) Derived(bar.foo);
  }
}

When we call the “new (this) Derived(bar.foo);” constructor, the Base object is already created and fields initialized. The repeated constructor call will cause double initialization. A pointer to the newly allocated memory area will be written into ‘ptr’. As a result, we get a memory leak. The result of double initialization of an object of the std::vector type cannot be predicted at all. But one thing is obvious: such code is inadmissible.

Conclusion

An explicit constructor call is needed only in very rare cases. In common programming practice, an explicit constructor call usually appears due to a programmer’s wish to reduce the code’s size. Don’t do that! Create an ordinary initialization function.

This is how the correct code should look:

class CSlideBarGroup
{
  void Init(CString strName, INT iIconIndex,
            CListBoxST* pListBox);
public:
  CSlideBarGroup(CString strName, INT iIconIndex,
                 CListBoxST* pListBox)
  {
    Init(strName, iIconIndex, pListBox);
  }
  CSlideBarGroup(CSlideBarGroup& Group)
  {
    Init(Group.GetName(), Group.GetIconIndex(),
         Group.GetListBox());
  }
  ...
};

P.S. Explicit call of one constructor from the other in C++11 (delegation)

The new C++11 standard allows you to perform a call of constructors from other constructors (known as delegation). It allows you to create constructors that use behavior of other constructors without added code. This is an example of the correct code:

class MyClass {
  std::string m_s;
public:
    MyClass(std::string s) : m_s(s) {}
    MyClass() : MyClass("default") {}
};

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read