Programming

Complexity of std::list::size() is... O(N)

I want to share my recent "discovery" that was very shocking to me. Reading comments to a slashdot story about the C++0x standard I read something very interesting: std::list::size() has O(N) complexity in the gcc compiler.

Using Valgrind to debug memory leaks

Valgrind is a wonderful tool useful mainly to debug memory related problems in C/C++ programs. I don't know a better tool to find memory leaks. Although output of this program is often clear and intuitive it's worth to spend some time to get deeper knowledge of how Valgrind works, what exactly its messages mean and what are the problematic cases when tracing a memory leak is harder even with Valgrind.

C++ exception specifications are evil

When I began to program in C++ I was already experienced in C and had written some code in Java. After writing few thousands lines of code in this language I felt comfortable with all of it's goodies like templates, inheritance, operator overloading and (I thought so) exceptions. But I made at least one mistake: I used exception specifications. My intention was to use them as in Java: tell the compiler that this method throws specific exceptions so it can use this information to optimize code and tell me when I'm throwing/catching wrong exceptions. A great surprise was that in practice in most cases writing exception specifications slows the code and introduces bugs!

Pipe in bash can be a trap!

Today a colleague at work tried to debug a script in bash that didn't want to work as he expected. He hit one of traps people get into when writing bash scripts. Let's look at the code that find the largest message that was sent from this computer:

C++ object's reference counting

Last update: 13.01.2009

Passing objects to methods by pointer or reference is very handy if you want to boost performance by avoiding invoking the copy constructor. Sometimes it's the only way to pass an object because it doesn't have a copy constructor or assignment operator defined. An example would be an object that has an open operating system resource that can't be safely shared.

Let's say that you are creating a class that does some low-level operation on a file. It contains a file descriptor as a field:

  1. class LowLevelFile
  2. {
  3. int fd
  4.  
  5. // [...]
  6. };

Syndicate content