Archive for the ‘C++’ Category

C++ Style

Tuesday, 12 July 2011

There are plenty of resources concerning C++ coding style, or coding style more generally.

Here I describe some of my own programming style preferences that I have not seen described elsewhere.

(more…)

Advertisements

Boost DateTime, Locales and Facets

Saturday, 17 October 2009

Boost DateTime extends the standard library facets for formatting dates and times according to locale.

I have had some trouble with these libraries. It seems that there is something of a flaw in the design of the standard library locales. Following are some functions that look like they work but don’t, and one that work, but looks like it shouldn’t.

(more…)

Boost: converting xtime to posix_time

Saturday, 17 October 2009

Here is some Boost code to convert (approximately) from an xtime to a posix_time. (The function forgets the nanoseconds.)

#include <boost/thread/xtime.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
::boost::posix_time::ptime
  convert_to_posix_time (const ::boost::xtime & xt) // from_xtime
{
  // this conversion is valid until
  // about the year 2038 = 1970 + 2^31 seconds
  // after which long(xt.sec) will wrap
  ::boost::posix_time::ptime pt (
      ::boost::gregorian::date (1970, boost::gregorian::Jan, 1),
      ::boost::posix_time::seconds (long (xt.sec)) );
  return pt;
}

Fast Bit Reversal

Thursday, 15 October 2009

The naïve method of reversing the bits in a number copies the bits one-by-one in a loop, or alternatively, swaps them two-by-two. This algorithms is O(n) where n is the number of bits.

However, there is an O(log n) method (when n is a power of 2); rather than taking n or n/2 steps, it takes log2(n) steps.

(more…)

Memory Leaks

Tuesday, 29 September 2009

To check for possible memory leaks using MSVC:
_CrtDumpMemoryLeaks()

C/C++ Pre-processor Links

Friday, 25 September 2009

Some handy, partly Mi¢ro$oft/MSVC-specific, C/C++ pre-processor  links:

MS Visual C++ Pre-#defined Macros

MS Visual C++ #pragma Directives

STL Design Problem?

Thursday, 24 September 2009

Consider the following functions:

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/date_facet.hpp>
#include <clocale>

std::string format_date_crash (::boost::gregorian::date dt)
{
  std::locale locale_local ("");

  ::boost::gregorian::date_facet date_output;
  std::locale locale_adjusted (locale_local, &date_output);

  std::ostringstream date_ss;
  date_ss.imbue (locale_adjusted);

  p_date_output.format("%x");
  date_ss << dt;

  return date_ss.str();
}

std::string format_date_leakquery (::boost::gregorian::date dt)
{
  std::locale locale_local ("");

  ::boost::gregorian::date_facet * p_date_output
      = new ::boost::gregorian::date_facet;
  std::locale locale_adjusted (locale_local, p_date_output);

  std::ostringstream date_ss;
  date_ss.imbue (locale_adjusted);

  (*p_date_output).format("%x");
  date_ss << dt;

  // *** don't delete p_date_output ***
  return date_ss.str();
}

Here, I have used Boost too, but I don’t believe that the problem is in Boost.

Calls to format_date_crash() crash in the ~std::locale() destructor [oddly, I found that it crashes only on the second call].

On the other hand format_date_leakquery() looks as though it might leak: it has a new without a corresponding delete. However a test showed that it does not leak.

It seems that the std::locale constructor takes ownership of the facet, so that when the locale object is destroyed, so is the facet (ie. the facet is deleted).

Is this an instance of poor design in the STL, or is there some compelling reason that things are arranged this way?

C++: Hide-by-name Semantics and Signatures

Wednesday, 6 May 2009

I’ve been using C++ for many years, yet there’s something fundamental that I’ve only just learned: It concerns something called hide-by-name semantics.

(more…)

MSVC Projects, Source Control and GUIDs

Tuesday, 28 April 2009

Further to my post on build events in MSVC, I’ve noticed that sometimes MSDEV sometimes re-orders the GUIDs in a solution file.

I know a mouse and he hasn’t got a house;
I don’t know why I call him Gerald.
He’s getting rather old, but he’s a good mouse.

I have a simple Python script (sort of attached) that can read these files and sort those GUID sections into order.

(more…)

MSVC Projects, Source Control and Build Events

Monday, 20 April 2009

I think it is a good idea to move all the lines of events (e.g. post-build events) into a separate batch file.

(more…)