Page Collection for ^2011-10

2011-10-19 CPlusPlus0X

New keyword nullptr

Maybe what can be called a smaller correction in the new standard is the introduction of the keyword nullptr. In C++ 0 is allowed as the value for a null pointer. Often the macro NULL, defined to 0, is used to express that this is a pointer.

However 0 is of type int. So in a overloading scenario like this:

void func(int);
void func(char *);

The call, func(NULL), will call func(int)!

This might not be an overload situation that happens that often but still it is a problem. Maybe it is equally important that there now is a keyword that truly is a null pointer constant. No need to use NULL nor 0 any more for pointers.

Further nullptr has the semantics that you would expect with pointers. Here backward compatibility was regarded as important. So a nullptr in a bool context is false. Like this:

bool trueValue = !nullptr;
bool falseValue = nullptr;

Note that the constant 0 is still a valid null pointer constant for backwards compatibility reasons. So the nullptr keyword is not likely to break old code.

2011-10-20 Computers

What I didn't know about sed all these years!

When I first learned about Unix many moons ago sed(1), the stream editor, was one of the tools to explore. I didn't find it particularly useful until now. How many times have I not wanted to filter out only a part of a file or a stream. Without knowing better I have used tools like awk(1) but more often written scripts in perl or python to do the job.

And now, years later, yes even decades later, I find that it has been there all the time in sed, the range concept! Look here:

sed -n '/start-regexp/,/end-regexp/p'

does the trick. All that is needed is there! Here is the details.

  • -n is needed to silence the output from sed. Sed normally echoes every line it reads on stdin to stdout if not told otherwise.
  • The trailing "p" means that when the expression before it matches then print the line.
  • The expression before it is a range with a start condition and stop condition. So when the regular expression "start-regexp" finds a match in the stream sed starts to print lines and it all ends when the regular expression end-regexp finds a match. The last line that matches end-regexp is printed.

So /start-regexp/,/end-regexp/ defines a region in the stream and 'p' defines that it should be printed.

Voila! That is what we wanted. A simple way to select portions of a stream. Lets look at an example. This is how to get the output during one specific minute in the warn log:

sed -n '/Oct 20 15:59:/,/Oct 20 16:/p' < /var/log/warn

Really quite simple don't you think!