Articles

Creating Bad Code That's Still Great

There are plenty of opinions out there about how to write great code ranging from styles and techniques to code formatting and programming languages. Inevitably when discussing great code someone will say, "But what if you need code right now, and you are willing to sacrifice quality in order to get work done faster?"

Nobody Understands C++: Part 7: C++ Coding Standards

In case you find yourself asking where I get the stuff from that I blog about and wondering if I'm just making stuff up; many of the "Nobody Understands C++" articles on this website are inspired by principles found in the book "C++ Coding Standards" by Herb Sutter and Andrei Alexandrescu. The book is a collection of items from previous books that the pair has worked on together. As such, if you do not own any of the previous books this one makes a great addition to your library.

The book covers 101 best practices guidelines for your C++ code. It does not cover things like indentation level and style of comments, the things that we have all come to consider to be part and parcel with a standards document. In fact, it explicitly avoids talking about those things and says that they should be internally consistent within each file. "C++ Coding Standards" covers topics on program design, object lifetime management, namespaces, generic programming (templates) and the standard library.

Nobody Understands C++: Part 6: Are You Still Using Pointers?

It is best to avoid using pointers in C++ as much as possible. The use of pointers can lead to confusion of ownership which can directly or indirectly lead to memory leaks. Even if object ownership is well managed simple (and difficult to find) bugs can also lead to memory leaks.

Security Risks

Also, by avoiding the use of pointers we can avoid and sometimes eliminate common security holes such as buffer overruns. See the last example for more information about this.

Examples

Common instances were pointer usage can be avoided:

Why is ++i faster than i++ in C++?

I've heard this question come up a few times in C++ programming circles, "Why is ++i faster/better than i++?"

The short answer is: i++ has to make a copy of the object and ++i does not.

The long answer involves some code examples.

int i = 1;
int j = i++; // j is equal to 1, because i is incremented after the value is returned, 
              // which means a copy was made of the old value
int k = ++i; // k is equal to 3, because i is incremented, then returned. No copy is needed

Or a more robust example of the operator overloads:

Do You Know What ++i And i++ Really Do?

An interviewer who thinks he is being clever might present you with a code sample like the following and ask you what the output would be:

//C
#include <stdio.h>
 
void dosomething(int i, int j, int k, int l)
{
  printf("%d, %d, %d, %d\n", i,j,k,l);
}
 
int main(int argc, char **argv)
{
  int i =1;
  dosomething(i++, ++i, i++, ++i);
 
  i = 1;
  printf("%d\n", i++ + ++i + i++ + ++i);
 
  i = 1;
  printf("%d\n", (i++) + (++i) + (i++) + (++i));
}

C++ Loop Optimization: Part 3: Dynamic List Generation

I felt like this topic deserved one more article. (See part 1 and part 2 for the background.)

The question was posed, "what if you make the list of integers dynamically generated?"

So, I came up with this little generator iterator adaptor. It allows you to take any generator function and treat it like a forward iterator:

#include <iostream>
#include <algorithm>
 
template<typename Gen, typename Val, int Size>
class generator_itr
{
  private:
    Gen m_g;
    Val m_cur;
    int m_iteration;
 
  public:

Syndicate content