rulururu

post Empty blocks

October 27th, 2008

Filed under: C++ — Kai @ 10:54 am

A tricky loop I’d like to show you.

First of all a quick explanation of the two functions I use.

  • log2 computes the base-2 logarithm of x.
  • ceil rounds, it returns the smallest integral value that is not less than x.
/* compute the ceil(log2(x)); i,x are unsigned; x is not 0 */
for( i = x>>1, n = 0; i != 0; i >>= 1, n++ ) {
}

In C we can have loops that have nothing in their body. You should use braces around an empty body. This allows you to expand the body if necessary. Further it gives us a visual clue that something out-of-the-normal is going on.

post Making a program survive

October 15th, 2008

Filed under: C++ — Kai @ 7:49 am

VERIFY can be used for things that should never fail, though you may want to make sure you can provide better error recovery if the error can actually cause a crash in a production system.

The C language provides a macro, called assert, that is used to verify conditions that must be true at any point of the program. These include preconditions, postconditions and invariants, all of which are explained in introductory programming courses. Whenever an assertion is violated, the program is abruptly stopped because there is most likely a bug in its code.

Many programmers put ASSERT macros liberally throughout their code. This is usually a good idea. The nice thing about the ASSERT macro is that using it costs you nothing in the release version because the macro has an empty body. Simplistically, you can imagine the definition of the ASSERT macro as being

#ifdef _DEBUG
#define ASSERT(x) if( (x) == 0) report_assert_failure()
#else
#define ASSERT(x)
#endif

(The actual definition is more complex, but the details don’t matter here). This works fine when you are doing something like

ASSERT(whatever != NULL);

which is pretty simple, and omitting the computation of the test from the release version doesn’t hurt. But some people will write things like

ASSERT( (whatever = somefunction() ) != NULL);

which is going to fail utterly in the release version because the assignment is never done, because there is no code generated (we will defer the discussion of embedded assignments being fundamentally evil to some other essay yet to be written.

That’s what VERIFY is for. Imagine the definitions of VERIFY as being

#ifdef _DEBUG
#define VERIFY(x) if( (x) == 0) report_assert_failure()
#else
#define VERIFY(x) (x)
#endif

Note this is a very different definition. What is dropped out in the release version is the if-test, but the code is still executed.

You always have to keep in mind that in the release version of MFC, VERIFY evaluates the expression but does not print or interrupt the program. For example, if the expression is a function call, the call will be made.

At least this is an example how VERIFY can be used (codelines out of the tar-1.16 project)

/* Verify requirement R at compile-time, as an integer constant expression.
   return 1.  */
 
# ifdef __cplusplus
template <int w>
  struct verify_type__ { unsigned int verify_error_if_negative_size__: w; };
#  define verify_true(R) \
     (!!sizeof (verify_type__<(R) ? 1 : -1>))
# else
#  define verify_true(R) \
     (!!sizeof \
      (struct { unsigned int verify_error_if_negative_size__: (R) ? 1 : -1; }))
# endif
 
/* Verify requirement R at compile-time, as a declaration without a
   trailing ';'.  */
 
# define verify(R) extern int (* verify_function__ (void)) [verify_true (R)]
 
#endif

post Mail Goggles will help to avoid embarrassing emails

October 8th, 2008

Filed under: Internet — Kai @ 3:21 pm

Mail Goggles, available at Google Labs, tries to prevent drunk e-mailing.

Google Mail has invented a way of stopping you sending emails that may later prove embarrassing.

Intended to help you overcome the urge to say what you really think late on a Friday night when you are a little the worse for wear, Google’s Mail Goggles asks you to solve “a few simple math problems after you click send to verify you’re in the right state of mind”.

Of course, you have to enable the feature first – Google isn’t trying to force you to take an unwelcome arithmetic test every time you want to send a message – and even if you do enable it, it doesn’t assume you’re drunk all the time.

“By default, Mail Goggles is only active late night on the weekend as that is the time you’re most likely to need it. Once enabled, you can adjust when it’s active in the General settings,” said Jon Perlow, a Gmail engineer.

Note: You can activate the feature in your Gmail or Google Mail account by clicking on the Settings tab on the top right of the screen and selecting Labs. Scroll down and you should find a brief explanation of the Mail Goggles feature and the option to enable it.

ruldrurd
Powered by WordPress, Content and Design by Kai Bellmann
Entries (RSS) and Comments (RSS)