    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Code Critique Competition 120</title>
        <link>https://members.accu.org/index.php/journals/2712</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">CVu Journal Vol 31, #5 - November 2019 + Student Code Critiques from CVu journal.</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c404/">315</a>
                    (7)
<br />

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c184/">Journal Columns</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c183/">Code Critique</a>
                    (70)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c404-183/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c404+183/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Code Critique Competition 120</h1>
<p><strong>Author:</strong>&nbsp;Bob Schmidt</p>
<p>
<strong>Date:</strong> 02 November 2019 18:07:28 +00:00 or Sat, 02 November 2019 18:07:28 +00:00</p>
<p><strong>Summary:</strong>&nbsp;Set and collated by Roger Orr. A book prize is awarded for the best entry.</p>
<p><strong>Body:</strong>&nbsp;<p class="EditorIntro">Please note that participation in this competition is open to all members, whether novice or expert. Readers are also encouraged to comment on published entries, and to supply their own possible code samples for the competition (in any common programming language) to scc@accu.org.</p>

<p class="EditorIntro">Note: if you would rather not have your critique visible online please inform me. (Email addresses are not publicly visible.)</p>

<h2>Last issueâ€™s code</h2>

<p class="blockquote">Iâ€™m relatively new to C++ and Iâ€™m trying to build up a simple hierarchy of different entities, where their name and id and a few more characteristics are held in an <code>Entity</code> base class; and specific subclasses, such as <code>Widget</code> in the code below, hold the more specialised data â€“ in this case simulated by the member name <code>data</code>. Iâ€™ve been having problems with the program Iâ€™m writing crashing and so Iâ€™ve simplified it down to a short example which usually produces output like this:</p>

<pre class="programlisting">
     Test
     none
     Another widget
     &lt;core dump&gt;</pre>

<p class="blockquote">Please can you suggest what might be causing the core dump? Iâ€™ve had to use <code>-fpermissive</code> with gcc (but not msvc) â€“ but surely thatâ€™s not causing the problem?</p>

<p>Listing 1 contains <span class="filename">Entity.h</span>, Listing 2 is <span class="filename">Widget.h</span> and Listing 3 is <span class="filename">example.cpp</span>. As always, there are a number of other things you might want to draw to the authorâ€™s attention as well as the presenting problem.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
#pragma once
class Entity
{

private: 
  char* pName{ NULL }; 
  int idNumber{ 0 };

public:
  void Setup(char const* name)
  {
    pName = strdup(name);
  }
  virtual ~Entity()
  {
    delete pName;
  }
  // Delete the current object using the dtor,
  // and re-create as a blank object
  void Reset()
  {
    this-&gt;~Entity();
    new(this) Entity();
  }
  char *Name() 
  {
    return pName ? pName : &quot;none&quot;;
  }
};
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 1</td>
	</tr>
</table>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
#pragma once
class Widget : public Entity
{
  int* data;
public:
  Widget() { data = new int[10]; }
  ~Widget() { delete data; }
};
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 2</td>
	</tr>
</table>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
#include &lt;iostream&gt;
#include &lt;memory&gt;
#include &lt;string.h&gt;

#include &quot;Entity.h&quot;
#include &quot;Widget.h&quot;

int main()
{
  Widget w;
  w.Setup(&quot;Test&quot;);
  std::cout &lt;&lt; w.Name() &lt;&lt; '\n';
  w.Reset();
  std::cout &lt;&lt; w.Name() &lt;&lt; '\n';
  w.Setup(&quot;Another widget&quot;);
  std::cout &lt;&lt; w.Name() &lt;&lt; '\n';
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 3</td>
	</tr>
</table>

<h2>Critiques</h2>

<h3>Martin Janzen</h3>

<h4>Overly permissive?</h4>

<p>The author is right about one thing: the error being masked by gccâ€™s <code>-fpermissive</code> flag is not the cause of the core dump. The compiler is unhappy with the <code>Entity::Name()</code> method, which can return a non-const pointer to a string literal. Changing the return type to <code>const char*</code> (or <code>char const*</code>, to be trendy) removes the warning and makes the code safer, as it prevents the caller from attempting to overwrite <code>pName</code>â€™s memory â€“ or the literal! â€“ through the returned <code>char*</code>.</p>

<p>The <code>Name()</code> method itself ought to be const too, as itâ€™s clearly not intended to allow modifications to the Entity.</p>

<h4>Placement new?!</h4>

<p>Instead, the core dump is caused by the bizarre behaviour of <code>Entity::Reset()</code>, which invokes the objectâ€™s dtor manually, then uses placement <code>new</code> to initialize a new <code>Entity</code>, for reasons known only to the author.</p>

<p>The problem is, <code>Entity</code> is meant to be used as a base class. Since its dtor is virtual, the call to <code>this-&gt;~Entity()</code> correctly causes the derived classâ€™s dtor to be called. But <code>Reset()</code> then has no way to re-create the derived class; it can initialize only the <code>Entity</code> slice of the object.</p>

<p>In this example, <code>Widget</code> has allocated an array pointed to by <code>data</code>, which is destroyed by the explicit dtor call. But only the <code>Entity</code> ctor is called, not the <code>Widget</code> ctor; so the value of <code>data</code> is not reset. Therefore, when <code>Widget</code> goes out of scope in <code>main()</code>, <code>data</code> is deleted a second time, resulting in the core dump.</p>

<p>(This is easy to diagnose by running the code under valgrind or, in gcc or clang, by enabling ASAN with the <code>-fsanitize=address</code> flag. Either tool will show the stack trace at the time of each delete call, making it clear what happened.)</p>

<p>To fix this, <code>Entity::Reset()</code> shouldnâ€™t try to get so fancy: it should just clear its data members. Also, it should be made <code>virtual</code> so that when the object is to be reset to its initial state, derived classes get a chance to reset their own data members too.</p>

<p>Or, we could follow Herb Sutterâ€™s â€˜Non-Virtual Interfaceâ€™ idiom, keeping the public <code>Reset()</code> method non-virtual in the base class, but calling a private <code>virtual ResetImpl()</code> which the derived classes can override.</p>

<p>(Some programmers will try to invent other clever alternatives, using CRTP and the like. That seems like overkill, and an invitation for trouble, especially given that the author is â€œ<em>relatively new to C++</em>â€.)</p>

<h4>C vs. modern C++ style</h4>

<p>The real question, though, is why all of this allocation and deletion is happening in the first place. As weâ€™ve seen, itâ€™s error-prone in even a simple example, and completely unnecessary in this enlightened age.</p>

<p>Most importantly, the <code>Entity::pName</code> pointer should be replaced with a <code>std::string</code>. This single change:</p>

<ul>
	<li>manages the memory for the name string safely and automatically;</li>
	
	<li>eliminates the memory leak which now occurs if <code>Setup()</code> is called twice;</li>
	
	<li>permits the dtor to be replaced by a simple <code>=default</code>; and,</li>
	
	<li>if the name is short enough for the small string optimization to be used, may even make the code run faster, with no allocations at all.</li>
</ul>

<p>(Thereâ€™s a whole discussion that could be had about how best to handle sink arguments, as for <code>Setup()</code>. If this method were modified to take a <code>std::string</code> argument rather than a <code>char const*</code>, the argument could be moved into <code>Entity::name</code> rather than copied, possibly allowing the compiler to do even more optimization. But for now, letâ€™s assume that the interface is to be changed as little as possible.)</p>

<p>In the derived class, the <code>Widget::data</code> array has a fixed size, so it can be replaced by a C-style array or <code>std::array</code> member rather than allocating it on the heap. This allows both the ctor and dtor to be replaced with <code>=default</code>. Also, removing the unnecessary indirection makes the code just a bit more cache-friendly.</p>

<h4>Basic hygiene</h4>

<p>The <span class="filename">example.cpp</span> test program shouldnâ€™t have to know that it needs to include <code>&lt;memory&gt;</code> and <code>&lt;string.h&gt;</code> in order to use <code>Entity</code> and <code>Widget</code>. Each classâ€™s header should <code>#include</code> all of its dependencies, so that a would-be user doesnâ€™t have to worry about the finer points of the implementation (which, as weâ€™ve seen, can and should change).</p>

<p>Since <code>Entity</code> appears to be intended as an abstract base class but has no pure virtual methods, its ctor could be given protected access so that it can be called only from derived classes; no standalone <code>Entity</code> instances can be created.</p>

<p>Speaking of access, <code>Entity::idNumber</code> is initialized to 0, but it has private access and is never touched in the <code>Entity</code> base class. It needs to be generated somehow by <code>Entity</code>, or given protected access, or simply removed; itâ€™s of no use to anyone right now. Similarly, <code>Widget::data</code> is also private but not used. But presumably this code is simply an early experiment, with more methods to be added later.</p>

<p>I wonâ€™t waste any time on coding style except to note that the capitalized method names are a bit unusual and archaic-looking. As weâ€™re trying to minimize changes to the interface, they can be left as is.</p>

<h4>Learning the craft</h4>

<p>Finally, I think itâ€™d be worthwhile to offer a couple of bits of general advice to the author.</p>

<ul>
	<li>The use of <code>strdup()</code>, etc. suggests a C programming background, and/or some 1990s C++. It would be worth spending some time with one or two more recent books, such as <em>A Tour of C++</em> (the 2nd edition) and <em>Effective Modern C++</em>; also, at the <em>C++ Core Guidelines</em>, and at Bjarneâ€™s homepage at <a href="www.stroustrup.com">www.stroustrup.com</a>, which has a couple of papers on the subject of how to write good modern C++.</li>
	
	<li>Most C++ compilers today are very, very good. As a rule, itâ€™s best to never ignore their warnings unless you are absolutely sure you understand the reasons both for the warning and for why it should be ignored in a specific situation. There is hardly ever a good excuse for this; itâ€™s good practice to strive for zero warnings.</li>
	
	<li>The <code>-fpermissive</code> flag, in particular, is a flagrant admission that your code is broken. It should never be needed when writing new code.</li>
	
	<li>Learn to use tools like valgrind and ASAN: they can save you from hours or days of frustration, or much worse.</li>
	
	<li>Finally, given how rapidly and substantially the C++ language is changing, do make sure to remain current by taking advantage of the endless resources available today: conferences, books, videos, web sites, courses, podcasts â€“ and of course ACCU publications! </li>
</ul>

<p class="EditorIntro">[ED: Full source code was supplied, but I elided it to save space.]</p>

<h3>Hans Vredeveld</h3>

<p>Before we look into the core dump, letâ€™s look into the need for <code>-fpermissive</code>. With <code>-fpermissive</code>, you tell the compiler to ignore a group of errors and just compile it. Hopefully it will not bite you at runtime. In this case, <code>-fpermissive</code> is needed because the function <code>Entity::Name</code> can return the constant <code>none</code> (type <code>char const*</code>), while it advertises to return a (modifiable) <code>char*</code>. Just make it return a (non-modifiable) <code>char const*</code> and this problem is solved. Also, the function itself can be made <code>const</code>, giving the signature:</p>

<pre class="programlisting">
  char const* Name() const</pre>

<p>On to the core dump. Running the program on my Linux system with the environment variable <code>MALLOC_CHECK</code> set to 3 gives some information about what is going wrong:</p>

<pre class="programlisting">
  *** Error in `./cc119': free(): invalid pointer:
  0x00000000014e5c20 ***
  Aborted (core dumped)</pre>

<p>Apparently, we try to <code>free</code> some memory that canâ€™t be <code>free</code>d. For further investigation, letâ€™s build the program with address sanitizer (<code>-fsanitizer=address</code> in gcc/clang) and run it. The first thing address sanitizer complains about is a mismatch between <code>operator new[] </code>and <code>operator delete</code>. Indeed, in <code>Widget</code>â€™s constructor, we <code>new[]</code> an array, and in the destructor we <code>delete</code> a single object. Changing <code>delete</code> to <code>delete[]</code> in the destructor solves this issue.</p>

<p>Building the program again with address sanitizer and running it, shows that address sanitizer is not done with us. Now it complains that we used <code>malloc</code> to allocate memory and <code>delete</code> to deallocate it. The <code>malloc</code> is hidden in <code>strdup</code> in <code>Entity::Setup</code>. We can fix this by changing <code>delete pName;</code> in <code>Entity</code>â€™s destructor to <code>free pName;</code>. (Even better: change the type of <code>pName</code> from <code>char*</code> to <code>std::string</code>. Also rename it to <code>name</code> in that case.)</p>

<p>Still, we havenâ€™t found the reason for the core dump. Build it a third time with address sanitizer and run it. Now address sanitizer complains that we are attempting a double-free. In other words, we try to <code>free</code> something that we <code>free</code>d before. To understand why, we have to look at what happens when we call <code>w.Reset()</code> on a <code>Widget w</code>. Before we call <code>w.Reset()</code> and after we constructed <code>w</code>, <code>w.data</code> is pointing to some allocated memory. Now, when we call <code>w.Reset()</code>, we first destruct <code>w</code> (via a call to the virtual destructor of <code>Entity</code>). The memory pointed to by <code>w.data</code> is deallocated, but the pointer itself is not changed. Next in <code>Reset()</code>, a new <code>Entity</code> object is constructed by the placement <code>new</code>. This placement <code>new</code> knows nothing about inheritance and does not construct a <code>Widget</code>. The memory previously occupied by <code>w.data</code> is left untouched and when interpreting the new object as a <code>Widget</code>, it contains the same <code>w.data</code> that points at unallocated memory. When <code>w</code> finally goes out of scope, we are going to destruct it as if it is a <code>Widget</code>, resulting in trying to delete the unallocated memory that <code>w.data</code> is pointing to.</p>

<p>We could solve this problem by making <code>Reset</code> a virtual function in <code>Entity</code> and implementing it in <code>Widget</code>, but it will be hard to create something that is robust and easy to understand and maintain. Before we continue, letâ€™s have a look at what the code really means.</p>

<p>In <code>main()</code> we begin with declaring and default constructing a <code>Widget w</code>. This <code>w</code> does not have a name, and conveys the meaning of no widget. It is just a placeholder for a widget. Next we call <code>w.Setup(&quot;Test&quot;)</code>, which converts <code>w</code> into â€˜Testâ€™ widget. This is the moment that we actually construct a <code>Widget</code>. Following this, we call <code>w.Reset()</code>. This destructs the widget <code>w</code> and changes it again into an empty placeholder. With the call to <code>w.Setup(&quot;Another widget&quot;)</code> we construct â€˜Anotherâ€™ widget at <code>w</code>. Finally, when <code>w</code> goes out of scope, we destroy the widget for the last time.</p>

<p>This analysis of the code makes clear that <code>Widget</code> has two functions:</p>

<ol>
	<li>It is a container of size at most one for <code>Widget</code>s.</li>
	<li>It is a widget, but not a specific one. It represents widgets of different types.</li>
</ol>

<p>Note that I talk about widgets here, but that everything I talk about is not implemented by the class <code>Widget</code>, but by <code>Entity</code>.</p>

<p>We can improve the code by separating the two functions. The container function can be implemented easily with an <code>std::unique_ptr&lt;Entity&gt;</code>. Now <code>Entity</code> no longer needs the functions <code>Setup</code> and <code>Reset</code>, but it will need a constructor:</p>

<pre class="programlisting">
  class Entity
  {
  private:
    std::string name;
    int idNumber{ 0 };
  public:
    explicit Entity(std::string name_) :
      name{name_}
    { }
    virtual ~Entity() = default;
    std::string const&amp; Name() const
    {
      return name;
    }
  };</pre>

<p>Instead of a single class <code>Widget</code> that has to represent both a test widget and another widget, I would create two classes <code>TestWidget</code> and <code>AnotherWidget</code>. In this test program they will have similar implementations. For <code>TestWidget</code> this is (also changing its data member, to simplify its use):</p>

<pre class="programlisting">
  class TestWidget : public Entity
  {
    int data[10];
  public:
    TestWidget() : Entity(&quot;Test&quot;)
    { }
  };</pre>

<p>Some attention must be given to copying and moving <code>Entity</code>s and <code>Widget</code>s. This is left as an exercise for the reader.</p>

<p>We can now rewrite <code>main()</code> as:</p>

<pre class="programlisting">
  int main()
  {
    std::unique_ptr&lt;Entity&gt; w =
      std::make_unique&lt;TestWidget&gt;();
    std::cout &lt;&lt; w-&gt;Name() &lt;&lt; '\n';
    w.reset();
    //Error: std::cout &lt;&lt; w-&gt;Name() &lt;&lt; '\n';
    w = std::make_unique&lt;AnotherWidget&gt;();
    std::cout &lt;&lt; w-&gt;Name() &lt;&lt; '\n';
  }</pre>

<p>Finally, we have to clean up the includes. <span class="filename">Widget.h</span> has to include <code>&quot;Entity.h&quot;</code>, <span class="filename">Entity.h</span> has to include <code>&lt;string&gt;</code>, and <span class="filename">example.cpp</span> has to include <code>&quot;Entity.h&quot;</code>, <code>&quot;Widget.h&quot;</code>, <code>&lt;memory&gt;</code> (which was not needed in the original code) and <code>&lt;iostream&gt;</code>. The include for <code>&lt;string.h&gt;</code> is not needed any more (originally, it was needed in <span class="filename">Entity.h</span>, not in <span class="filename">example.cpp</span>).</p>

<h3>Marcel MarrÃ© and Jan Eisenhauer</h3>

<p>While the reason for gccâ€™s complaint that <code>-fpermissive</code> must be used is indeed not the cause for the core dump, any programmer is generally ill-advised to ignore warnings. In this case, <code>Entity::Name()</code> returns a non-const <code>char*</code>, but in case of a <code>nullptr</code> in <code>pname</code>, a pointer to the literal <code>&quot;none&quot;</code> is returned, which is by definition <code>const</code>. It is also good style to use <code>const</code> for getters, so the user knows an objectâ€™s state is not changed by calling it. There is no need to change the body of the method, but the signature is best changed to <code>char const *Name() const</code>.</p>

<p>Let us now look at the reason for the core dump. With <code>Widget w;</code> in <code>main()</code>, an object of class <code>Widget</code> is created and the compiler knows that it needs to call its destructor <code>~Widget</code> when it goes out of scope. In <code>Entity::Reset()</code> the widget destructor is called for this object, because we have a virtual destructor and invoke it through a pointer. However, only an <code>Entity</code> object is created in the <code>Widget</code>â€™s place. When <code>w</code> goes out of scope at the end of <code>main()</code>, the compiler does not use the virtual destructor, however, because (it thinks) it knows the precise type of <code>w</code>, since it was created on the stack. (Polymorphism in C++ works only with references and pointers, not with concrete objects.) Hence, <code>~Widget </code>is used on an object whose type is <code>Entity</code>, and in particular <code>Widget::data</code> is deleted twice, likely causing the core dump. (Incidentally, the code did not core dump on our machine, showing how insidious bugs due to undefined behaviour can be.)</p>

<p>There is a multitude of ways to fix this problem. With the code given it is obviously difficult to see why the author felt the need to re-use objects. If the main fear is that heap allocations and deallocations are slow, from C++17 onwards other memory allocators exist that can be used. Another possibility that eschews heap allocations is <code>virtual Reset()</code> and every class that introduces additional members overriding it with its own and calling their ancestorâ€™s <code>Reset()</code>. This can be error prone (both due to forgetting to implement <code>Reset()</code> for a child class requiring it and due to forgetting to call the immediate base classâ€™s <code>Reset()</code> and the author should make sure the effort is worth the payoff. Depending on the use case, there are other approaches that can be used, such as an Entity-Component-System (see <a href="https://en.wikipedia.org/wiki/Entity_component_system">https://en.wikipedia.org/wiki/Entity_component_system</a>), that do not use inheritance and thus do not present the same problem.</p>

<p>There are a few further points on the code.</p>

<p>Both <code>Entity</code> and <code>Widget</code> have compiler-generated copy constructor and copy assignment, but when one makes a copy, a shallow copy is created, meaning that only the pointers are copied, but not the content pointed to (which would be a deep copy). Someone expecting to be able to make a copy of an object and then be able to change one without affecting the other will find that this works for <code>idNumber</code>, but not for <code>pName</code> or <code>data</code>. Additionally, the latter membersâ€™ destructors would be called twice. Note also that copying can be dangerous in the case of a hierarchy, because assigning, e.g., a <code>Widget</code> object to an <code>Entity</code> object will lead to object slicing (see <a href="https://en.wikipedia.org/wiki/Object_slicing">https://en.wikipedia.org/wiki/Object_slicing</a>).</p>

<p>It is good style to avoid raw pointers to benefit from automatically working copy- and move-members. Hence, use <code>std::string</code> instead of <code>char*</code> for <code>pName</code> and <code>std::array&lt;int, 10&gt;</code> for <code>data</code>. See <em>CppCoreGuideline</em> R.1 (Manage resources automatically using resource handles and RAII), R.5 (Prefer scoped objects; donâ€™t heap-allocate unnecessarily) and R.11 (Avoid calling new and delete explicitly).</p>

<p>It is a good idea for headers to include standard and other own headers they require; in the authorâ€™s code neither does <span class="filename">Widget.h</span> include the base class header, nor does <span class="filename">Entity.h</span> include the string header it needs, although the main code file does not itself require it directly.</p>

<p>As a matter of self-documenting code, change the signature of the subclass destructor to <code>~Widget() override</code>.</p>

<p>Another minor problem is that the string literal <code>&quot;none&quot;</code> is not technically an invalid name for an <code>Entity</code>. With <code>std::string</code>, an empty string could indicate an unnamed <code>Entity</code>. Another method is to make the possibility of a missing value explicit by using <code>std::optional</code> (since C++17: <code>boost::optional</code> was available before).</p>

<h3>James Holland</h3>

<p>My first impressions are that the code seems to be a mixture of C and C++. Perhaps the student comes from a C background and has not yet learnt the C++ way of doing things. I shall say more about this later but for now letâ€™s persevere with the code presented.</p>

<p>When compiling the code on my system I get one error, namely that an object of type <code>const char *</code> cannot be used to initialise a return object of type <code>char *</code> in function <code>Name()</code>. This is because a string literal is considered <code>const</code> and allowing the conversion to non-const would enable the string literal to be modified which would not make sense; hence the error message. Perhaps the simplest way to overcome this problem is to make the return type of <code>Name()</code> into <code>const char *</code>. The code now compiles without error. There are still problems, however.</p>

<p>I notice the student is using a function named <code>strdup()</code>. After a little research I discover this function has recently been added to the C language. According to cppreference, the function duplicates the string pointed to by its parameter and returns a pointer that must be <code>free</code>d using the function <code>free()</code>. An inspection of the studentâ€™s code reveals that <code>delete</code> is being used. This will result in undefined behaviour.</p>

<p>There is a similar problem with the destructor of <code>Widget</code>. <code>Widget</code>â€™s constructor creates an array of 10 <code>int</code>s on the heap using <code>new[]</code>. The destructor, therefore, needs to call <code>delete[]</code>, not <code>delete</code>. It should also be noted that the variable <code>idNumber</code> is not used in the sample code.</p>

<p>Running the revised code using Clang Address Sanitiser results in a message stating that data is being deleted twice. This would result in undefined behaviour. A quick and simple way of resolving this problem is to assign data the value <code>nullptr</code> after deleting <code>data</code>. The program now produces the required output. Unfortunately, the design of the program leaves a lot to be desired.</p>

<p>Some minor improvements could be made to the software. There is a superfluous set of braces placed on the end of the placement <code>new</code> statement. It is better to assign <code>pName</code> the value <code>nullptr</code> rather than <code>NULL</code>.</p>

<p>More seriously, the <code>Reset()</code> function creates a new version of <code>Entity</code> in the same memory location as the previous incarnation of <code>Entity</code> but not before calling <code>Entity</code>â€™s destructor. This will result in undefined behaviour as the new object resides in unallocated memory and may be overwritten at any moment by some other part of the system. Perhaps the direct call to the destructor of <code>Entity</code> could be removed but this results in memory leaks. To cure the memory leak problem <code>pName</code> needs to be freed. This may well result in code that works perfectly but things are getting very complicated and difficult to understand.</p>

<p>The student states that he or she is relatively new to C++ and this shows in the construction of the code. In many ways, C++ is quite a simple language once certain rules and idioms have been studied and understood.  The best way to develop a C++ program is to take advantage of the facilities the programming language provides. Advantage can be taken of constructors, for example, to that objects can be created and initialised in a single statement. This will make functions such as <code>Setup()</code> unnecessary. Text strings should employ <code>std::string</code> instead of creating and using strings based on pointers to chars. The main feature of <code>std::string</code> is that they look after themselves. They will allocate storage on the heap, perform any initialisation and relinquish heap memory when they go out of scope. They have many more features that make them simple and safe to use. Try to use a higher level of abstraction.</p>

<p>The student appears to be attempting to reuse memory when a new <code>Widget</code> objects are required. In such cases a new object should be created. The old object should be left to be destroyed when it goes out of scope. There is rarely a need to destroy objects explicitly. It is important not to be too clever when writing software. The simplest approach is usually the best.</p>

<p>Finally, I provide a simple program that operates in a similar way to the studentâ€™s but uses higher level features that are provided by the C++ standard.</p>

<pre class="programlisting">
  #include &lt;array&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  class Entity
  {
    public:
    Entity(const char * new_name)
    : entity_name(new_name){}
    std::string entity_name;
    int ID_number{0};
  };
  class Widget : public Entity
  {
    public:
    Widget(const char * widget_name)
    : Entity(widget_name){}
    void reset()
    {
      entity_name.clear();
      ID_number = 0;
    }
    void setup(const char * new_name)
    {
      entity_name = new_name;
    }
    std::array&lt;int, 10&gt; data;
    std::string name() const
    {
      return entity_name.empty() ? &quot;none&quot; :
        entity_name;
    }
  };
  int main()
  {
    Widget w(&quot;Test&quot;);
    std::cout &lt;&lt; w.name() &lt;&lt; '\n';
    w.reset();
    std::cout &lt;&lt; w.name() &lt;&lt; '\n';
    w.setup(&quot;Another widget&quot;);
    std::cout &lt;&lt; w.name() &lt;&lt; '\n';
  }</pre>

<h2>Commentary</h2>

<p>The fundamental issue with this code, as all the critiques explained, is the explicit invocation of the destructor in <code>this-&gt;~Entity();</code>. The comment for the <code>Reset()</code> method explains that the intent is to replace the current object with a blank one.</p>

<p>The trouble is that the static types of the object deleted and the new object created do not match. The destructor is <code>virtual</code> and so the call <code>this-&gt;~Entity();</code> actually invokes <code>Widget::~Widget()</code> and so destroys the <code>Widget</code> object. When the variable <code>w</code> goes out of scope at the end of <code>main</code>, the <code>Widget</code> destructor is called but the actual type of the object being destroyed is <code>Entity</code> and so the behaviour is undefined.</p>

<p>One â€˜solutionâ€™ would be to just destroy the <code>Entity</code> part of the object, but while this can be done (<code>this-&gt;Entity::~Entity();</code>) it doesnâ€™t help achieve the goal of the <code>Reset()</code> method. The question is whether <code>Reset()</code> is intended to be used to create <em>another</em> object or simply to re-initialise the <em>existing</em> one. At this point youâ€™d probably need to talk to the original author to find out what their intended use cases are.</p>

<p>As James noted, this â€˜two-phaseâ€™ construction via a constructor and a <code>Setup()</code> method not idiomatic C++. It would usually be better to provide the name at construction and create a complete object.</p>

<p>As Hans pointed out, you can argue that we appear to have a single class (<code>Entity</code>) with two different responsibilities â€“ it is both a container for an optional object (hence the <code>Name</code> is <code>&quot;none&quot;</code> when the entity is not yet populated by a call to <code>Setup()</code>) <em>and</em> a base class for a class hierarchy</p>

<h2>The winner of CC 119</h2>

<p>All the critiques identified the root cause of the presenting problem â€“ the core dump â€“ and also gave an explanation of the compiler warning.</p>

<p>There were various suggestions for how to improve the design (as well as more detailed suggestions on places where the code was more C than C++)</p>

<p>Hans and James both pointed out the memory allocation/deallocation mismatches (<code>malloc</code> + <code>new[]</code> being incorrectly paired with <code>delete</code>) and every critique included suggestions to more use <code>std::string</code> and an array or <code>std::array</code> to remove the explicit memory management completely.</p>

<p>Several submissions also recommended using an address sanitizer or a memory checker as ways to help the person to solve their own problem. There are a wide variety of tools now available for this sort of assistance, and I suspect many newer programmers will need some guidance on how to pick the correct one for a given problem. Martinâ€™s recommendations under â€˜Learning the craftâ€™ would be a good place to start!</p>

<p>I found this a hard critique to judge since there were so many strong entries â€“ thanks to all for your work in providing our readers with food for thought. However, in my opinion Martin provided the best entry for this critique by a short head.</p>

<h2>Code Critique 120</h2>

<p>(Submissions to scc@accu.org by December 1st)</p>

<p class="blockquote">Iâ€™m doing some simple text processing to extract sentences from a text document and produce each one on a separate line. Iâ€™m getting a space at the start of the lines and wonder whatâ€™s the best way to change the regexes to remove it?&quot;</p>

<pre class="programlisting">
    $ cat input.txt
    This is the first sentence. And the
    second. This is
    the
    last
    one.
    
    $ sentences input.txt
    This is the first sentence.
     And the second.
     This is the last one.</pre>

<p>Listing 4 contains the code. As always, there are a number of other things you might want to draw to the authorâ€™s attention as well as the presenting problem.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
#include &lt;fstream&gt;
#include &lt;iostream&gt;
#include &lt;iterator&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;
#include &lt;regex&gt;
using namespace std;

int main(int argc, char** argv)
{
  std::ifstream ifs(argv[1]);
  std::stringstream ss;
  char ch;
  while (ifs &gt;&gt; std::noskipws &gt;&gt; ch)
    ss &lt;&lt; ch;

  string s = ss.str();

  smatch m;

  while (regex_search(s, m,
    regex(&quot;[\\s\\S]*?\\.&quot;)))
  {
    std::string sentence(m[0]);
    std::regex whitepace(&quot;[\\s]+&quot;);
    std::regex_replace(
      std::ostream_iterator&lt;char&gt;(std::cout),
      sentence.begin(), sentence.end(),
      whitepace, &quot; &quot;);
    std::cout &lt;&lt; std::endl;
    s = m.suffix().str();
  }
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 4</td>
	</tr>
</table>

<p>You can also get the current problem from the accu-general mail list (next entry is posted around the last issueâ€™s deadline) or from the ACCU website (<a href="https://accu.org/index.php/journal">https://accu.org/index.php/journal</a>). This particularly helps overseas members who typically get the magazine much later than members in the UK and Europe.</p>

<p class="bio"><span class="author"><b>Roger Orr</b></span> Roger has been programming for over 20 years, most recently in C++ and Java for various investment banks in Canary Wharf and the City. He joined ACCU in 1999 and the BSI C++ panel in 2002.</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
