jeudi 24 avril 2014

The new VS’2013 may be a bit buggy …

In one hand I would say that moving from the old VS’2008 to the new VS’2013 was really fine, because even for a low-level programmer like me (in the project I’m working for, I only use C and C++) the editor’s improvements are really great !
But in another hand as in every software, there is still some bug. I only worked 2 or 3 days with VS’13 but I found 2 bug I reported through the MS Connect portal.
1) The C compiler has a terrible bug and I think that bug is a major issue, because it prevent the build of a tons of good C99 open source code.
for the VS’13 C compiler the following code is wrong and it report the following error:
typedef struct { int j; } test_t;

int f(test_t **p_pool, int i)
{
    if (i <= 0)
        return -1;

    test_t *pool;   
    *p_pool = pool;

    return i;
}



and the error is : 'test_t' : illegal use of this type as an expression

in fact, it looks like the compiler fails to interpret test_t as a type if the if-then statement body before isn’t surrounded by a some {}, for any other basic type, I mean int, char etc…. there is no problem but with all defined type containing “_t” (size_t, prtrdiff_t, uint8_t, etc….) it fails !

A workaround if to rewrite the code as:
typedef struct { int j; } test_t;

int f(test_t **p_pool, int i)
{
    if (i <= 0) {
        return -1;
    }

    test_t *pool;   
    *p_pool = pool;

    return i;
}



But it’s a pain to do that on a huge C/C99 code base.

Updated 2014 April 29th: An answer from MS dev team say that a fix will be available with the next update ... Good to hear!

2) the project property page has been improved with several new option, but switching from Debug to Release configuration directly from the property page may be disappointing, see below what’s may happen when you are doing a that.

Step to reproduce:


  • open a C/C++ project configuration property pages

  • switch the Configuration (Debug to Release or vice-versa)

  • now select another entry in the property tree and look. Below for example I was initially in Release / on C/C++->Preprocessor, I first switched to Debug and clicked on Language.

VS2013PropertyPagesBug1

Hopefully if you switch-back or if you close the pages and re-open then directly with “Debug” Configuration, the option will be back !

mercredi 16 avril 2014

The worst optimization I ever made….

As a programmer I often try to simplify the code and reduce the number of operation, but few days ago I made a big mistake.

In a video processing system, I replaced the following line, we use for each frame to compute its Presentation TimeStamp (a.k.a its PTS):

curr_pts = first_pts + nb_frame * duration;

by a simple addition

curr_pts += duration;
and curr_pts was initialized with first_pts.

Now let say that all those values are floating point number (double or float).
In the floating point number world, those 2 code aren’t really equivalent due to the error accumulation.

That problem is well known and a solution to minimize the error is the Kahan’s summation algorithm.

Take a look here on Ideone at a small test code I made to demonstrate it…..

But basically if we compute the delta between the 1st version and the 2nd version, we get 3.14042e-05 and with the Kahan’s algorithm we get only a delta of 1.74623e-09.

A big mistake and a big reminder that floating point computation aren’t so easy ….

lundi 7 avril 2014

Class/Struct definition local to a function.


You may know or not that defining a class or struct inside a function is possible but you may not be aware of all the issue you will meet when using that with the old C++98.
First thing, if you are using C++11 you can pass away, because in all case that feature will be correctly handle by recent compiler and debugger.
Now if your are still using a C++98 compiler or an old GCC, stay here and read the following:)

Definition in a function

In most case the following code will be Ok.

int function() { 
   typedef struct myStruct { 
       int num; 
       myStruct (int n): num(n){} 
       myStruct(const myStruct&amp; ro):num(ro.num){} 
    } mystruct_t; 

  mystruct_t t(2);

  return t.num;
} 


But want will happened if you try to use that struct with an STL’s container, like vector, deque, etc….. Example

int function() { 
   typedef struct myStruct { 
       int num; 
       myStruct (int n): num(n){} 
       myStruct(const myStruct&amp; ro):num(ro.num){} 
    } mystruct_t; 

  std::deque<mystruct_t> dq(10, mystruct_t(2));
  
   return dq[0].num;
} 

In fact here we have 2 different scenario:
  1. you are using GCC and it will not compile, because it’s not a C++98 compliant code
  2. you are using VC++2008 and it will compile, but you will meet weird debugger behavior on that piece of code.

Explanation

In C++98, you cannot use local class or struct with template type. This restriction is part of the standard. The reasoning was originally that local structures don't really have a names and thus it would be impossible to instantiate a template with them.

So as said if VC++2008 allow that code to compile, you will not be able to see what you have in your deque… Like in the snapshot below.

DebuggerError

My conclusion

Try to avoid that kind of code until you get the new C++11, because the portability across platform is not guarantee by the standard and that debugging and maintaining them is really hard. Define your class or struct outside of the function.