Home
Fractals
Tutorials
Books
Archive
My blog
My LinkedIn Profile

BOOKS i'm reading

Cryptography engineering, Niels Ferguson, Bruce Schneier, Tadayoshi Kohno, ISBN: 9780470474242
Advanced Programming in the UNIX(R) Environment (2nd Edition), W. Richard Stevens, Stephen A. Rago, ISBN:0201433079
Trading For a Living, Alexander Elder, ISBN:0471592242

mailto:olivier@olivierlanglois.net

Archives for: April 2008

04/30/08

Permalink 09:43:11 pm, by lano1106 Email , 284 words, 408 views   English (CA)
Categories: Video games

Amazon offers Xbox 360 GTA IV bundle discount

Amazon on Tuesday began offering a new retail discount for customers who purchase Microsoft Corp.'s Xbox 360 and Rockstar Games' Grand Theft Auto IV. In the promotion, customers who purchase an Xbox 360 Premium SKU or an Xbox 360 Elite SKU and GTA IV standard or premium edition for the Xbox 360 will receive a $30 discount off their order.

GTA IV rolled into U.S. retail stores on Tues. and is expected to sell nine million copies at launch. Analysts are expecting Grand Theft Auto IV to generate US$400 million in sales in its first week and to outsell last year's heavily hyped Halo 3 from Microsoft Corp., which took in US$300 million in its first week.

The Xbox 360 version will include two downloadable episodes available exclusively on Xbox Live. In a first, players can use the in-game phone to buy and download from Amazon MP3 tracks of songs they hear on the game's radio stations.

The premise could come from a novel or a movie: An Eastern European immigrant named Niko comes to the big city lured by his cousin's tales of easy wealth. Niko quickly discovers the truth and is left to make his own way in America, doing whatever it takes.

Take-Two Interactive in August 2007 delayed Grand Theft Auto IV to 2008 from an original October 2007 release. The publisher cited additional development time required to complete the title.

In March, Take-Two Interactive rejected a $2B buyout offer from EA, saying that the bid undervalued the software maker. Take-Two explained that it is still willing to open discussions with EA, but not until April 30, when Grand Theft Auto IV (for PlayStation 3 and Xbox 360) finally hits store shelves.

Grand Theft Auto IV is also available for the PS3.

04/28/08

Permalink 09:55:06 pm, by lano1106 Email , 680 words, 712 views   English (CA)
Categories: C++

ODR, inline functions and C++ compiler generated functions

ODR is the One Definition Rule. This is the rule that stipulates that a name (a function, a class or global variable and much more other types) must be defined not more than once in the same program. The compiler will emit an error if a name is defined more than once in the same translation unit (TU) or if the name is defined more than once but in differents TUs, it is the linker that will catch the error and it will emit a duplicate symbol error.

The ODR is the reason why header files guards are used (#ifndef XXX #define XXX #endif). If a header file was not protected by guards and included more than once in a TU, the ODR would be violated.

All C++ programmers have a formal knowledge of the ODR or at least an intuition of its details learned by experience but this rule gets very interesting when you start to study the exceptions to it. Studying these things makes you more appreciate the complexity of the C++ language.

For instance, there are the inline functions. Usually, when we think about inline functions, we consider them as fancy macros with strong type checks. However the inline keyword can be considered only as a hint given to the compiler and the compiler is free to completely ignore the inline specifier. If it does ignore it, it will generate a regular function and export it from the resulting object file. If the inline functions are defined in many TUs and linked together then according to the ODR, this would be an error but inline functions are exempted from the ODR and the linker only keeps one copy of the inline functions defined multiple times. I would be very eager to figure out how the linker knows that a given function is exempted from the ODR because as you will see next, there are no apparent hints to this effect.

The next exception to the ODR is the C++ compiler generated functions for a class. Those functions include the copy constructor and the assignment operator. The C++ standard says that these functions are always implicitly declared if not explicitly done by the user but implicitly defined only if they are used. They will also be considered as inline hence also exempted from the ODR. Another property of the generated functions is that they are classified as trivial or non-trivial. Essentially, a trivial copy constructor simply means that the class looks more like a C POD (Plain Old Data) structure than a class and the compiler can use the same C compiler technique to perform the deep copy (probably something like a memcpy). Otherwise, it will need to create a real constructor function. Here is a simple example to demonstrate these notions:

C.h: 

class C 
{ 
public: 
   C() : a(0), b(0), c(0), d(0), e(0) {} 
//  virtual ~C() {} 
private: 
   int a; 
   int b; 
   int c; 
   int d; 
   int e; 
}; 

f.cpp: 

#include "C.h" 

C f() 
{ 
     C a; 
     C b = a; 
     return b; 
} 

g.cpp: 

#include "C.h" 

C g() 
{ 
     C a; 
     C b = a; 
     return b; 
} 

main.cpp: 

#include "C.h" 

C f(); 
C g(); 

int main(int argc, char *argv[]) 
{ 
   f(); 
   g(); 
   return 0; 
} 

g++ -c f.cpp 
nm -a -C f.o 

00000000 t 
00000000 d 
00000000 b 
00000000 t 
00000000 n 
00000000 n 
00000000 a f.cpp 
00000000 T f() 
00000000 W C::C() 

No sign of the copy constructor. I am guessing that the compiler make the copy constructor inline because it is trivial. I am adding a virtual destructor to make the copy constructor non-trivial:

00000000 T f() 
          U operator delete(void*) 
00000000 W C::C(C const&) 
00000000 W C::C() 
00000000 W C::~C() 
00000000 W C::~C() 
00000000 V typeinfo for C 
00000000 V typeinfo name for C 
00000000 V vtable for C 
          U vtable for __cxxabiv1::__class_type_info 

Now g.o and f.o have a copy of C copy constructor and the linker will link fine and only one copy of the function will find its way in the final executable file because the implicitly defined copy constructor is inline.

04/23/08

Permalink 09:05:28 pm, by lano1106 Email , 67 words, 225 views   English (CA)
Categories: Video games

Additionnal PlayStation 3 game supporting Dual Shock 3 controller rumble feature

When Sony issued a press release announcing the new Dual Shock 3 controller, they included a list of games compatibles with the new controller.

I have just discovered that Stuntman : Ignition from THQ (which is not in the list) is also supporting the new Sony controller.

I did small search on the net and it does not seems to be a widely known fact, so here it is.

04/20/08

Permalink 05:25:41 pm, by lano1106 Email , 89 words, 200 views   English (CA)
Categories: Video games

GH3 replacement disks for the Wii are arriving

I have finally received my Guitar hero 3 replacement disk for the Wii. They are identical to the original disks except that there is a small star on the bottom of the fixed disk. I have taken a picture of the 2 disks side by side. The new disk is on the right. You should notice the small star on the disk to the right.

Wii guitar hero 3 replacement disk

The original disk and fixed disk are placed side by side to compare their differences.

I did not tried it yet to hear the sound improvement.

04/12/08

Permalink 03:22:31 pm, by lano1106 Email , 265 words, 276 views   English (CA)
Categories: C++

shift operator undefined behavior

I was expecting:

int main( int argc, char *argv[] ) 
{ 
   unsigned char m = 32; 
   register unsigned mask = (1<<m); 
   std::cout << std::hex << mask << '\n'; 
   return 0; 
} 

to print 0 but instead this program compiled with g++ (and VC++.NET2003 too) prints 1!

If I change (1<<m) by (1<<32) or if change the program for:

int main( int argc, char *argv[] ) 
{ 
   unsigned char m = 31; 
   register unsigned mask = (1<<m)<<1; 
   std::cout << std::hex << mask << '\n'; 
   return 0; 
} 

it gives me the expected 0.

In the C++ standard document, section 5.8. It is written

"The behavior is undefined if the right operand is negative, or greater than or equal to the length in bits of the promoted left operand."

The root for this behavior probably originates from C and the safe way to perform a bit shift when the number of bits to shift may exceed the length of the left operand is to implement the shift operation in a function:

Example, almost but not universally portable:

#include <climits> 

unsigned int safe_uint_shift(unsigned int value,
                             unsigned int bits) 
{ 
    if( bits > (CHAR_BIT*std::sizeof(unsigned int) )
    {
      return 0;
    }
    else
    {
        return value << bits; 
    }
}  

Put it in a header and make it inline if you like.

This solution has been proposed by Jack Klein.

The other way that I have used is to promote the left operand to an integer type having enough bits:

register unsigned mask =
  (static_cast<unsigned long long>(1)<<m)<<1;
Permalink 09:04:01 am, by lano1106 Email , 279 words, 167 views   English (CA)
Categories: C++

operator<< for a private inner class

Suppose we have

class A
{
  private:
  class B
  {
  };
};

and we would like to define operator<< for class B. I had this situation yesterday and it took me few tries to make it work. I first tried:

class A
{
...
};
std::ostream operator<<( ostream &, const A::B & );

It did not work because the compiler complained that B was private. My second attempt:

class A
{
  private:
  class B
  {
  };
  static std::ostream &operator<<( ostream &, const B & );
};

I was getting closer to the solution but this was still not quite right. Apparently, you do not have the right to declare operator<< as a static member of another class or as soon as an operator is defined as a class member, automatically, the compiler expect the left operand to be of this class type.

Then I tried this:

class A
{
  private:
  class B
  {
  };
  friend std::ostream &operator<<( ostream &, const B & );
};
std::ostream &operator<<( ostream &o, const A::B &b )
{
}

This time, I am not sure why but the linker now was complaining about duplicate symbols for my operator<< function. Not sure if it is the code that has a problem. It looks good to me. I suspect that maybe it is parameter type mangling problem and the compiler does not recognize 'const B &' as the same as 'const A::B &' but anyhow, I did not pursue the investigation I have finally made the code work like this:

class A
{
  private:
  class B
  {
  };
  friend std::ostream &operator<<( ostream &o, const B &b )
  {
  }
};

Olivier Langlois's blog

I want you to find in this blog informations about C++ programming that I had a hard time to find in the first place on the web.

April 2008
Sun Mon Tue Wed Thu Fri Sat
 << < Current> >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      

Search

Custom Search

XML Feeds

What is RSS?

Who's Online?

  • Guest Users: 4

powered by
b2evolution