Sunday, February 27, 2011

C++ to HTML

Sometimes, if you would show your C++ source code in a certain web site(e.g. blog), you'd better convert C++ source code into HTML format. Here is a good web site for this.

http://www.bedaux.net/cpp2html

The differences among auto_ptr, scoped_ptr and shared_ptr



#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <memory>
#include <stdio.h>

#include "Int.h" // include 4 bytes int class

typedef std::auto_ptr<Int>     auto_ptr;
typedef boost::scoped_ptr<Int> scoped_ptr;
typedef boost::shared_ptr<Int> shared_ptr;

void size()
{
  printf("sizeof(auto_ptr)   is %d\n", sizeof(auto_ptr));   // 4
  printf("sizeof(scoped_ptr) is %d\n", sizeof(scoped_ptr)); // 4
  printf("sizeof(shared_ptr) is %d\n", sizeof(shared_ptr)); // 8
}

void foo_ptr(auto_ptr ap)
{
  ap->foo();
}

void foo_ptr(scoped_ptr scp)
{
  scp->foo();
}

void foo_ptr(shared_ptr shp)
{
  shp->foo();
}

void foo()
{
  {
    //
    // auto_ptr
    //
    Int* pi = new Int(5);
    auto_ptr ap(pi);

    foo_ptr(ap);     // compile ok
    ap->foo();       // runtime error
  }

  {
    //
    // scoped_ptr
    //
    Int* pi = new Int(5);
    scoped_ptr scp(pi);

    // foo_ptr(scp); // compile error
    scp->foo();      // runtime ok
  }

  {
    //
    // scoped_ptr
    //
    Int* pi = new Int(5);
    shared_ptr shp(pi);
foo_ptr(shp);    // compile ok
    shp->foo();      // runtime ok
  }
}

int main()
{
  size();
  foo();
}

Download source code : http://sites.google.com/site/gilgildownload/download/IntTest2.zip

C++ copy constructor and assign operator



Can you distinguish between copy constructor and assign operator in C++ code? Here's a sample for you. Be aware of knowing that even if it's code format looks assign operator format, it is not assign operator format but copy constructor one.


Download source code : https://sites.google.com/site/gilgildownload/download/IntTest.zip


class Int
{
public:
  // constructor
  Int(const int i = 0)                 { printf("C...
  Int(const Int& r)                    { printf("C...


  // destructor
  virtual ~Int()                       { printf("D...


  // assign operator
  const Int& operator = (const int i)  { printf("A...
  const Int& operator = (const Int& r) { printf("A...
};


void foo()
{
  {
    Int a(1);  // C
    Int b(2);  // C
    Int c(a);  // C
  }


  {
    Int a(3);  // C
    Int b;     // C
    b = 4;     // A
    Int c;     // C
    c = a;     // A
  }


  {
    Int a(5);  // C
    Int b = 6; // C // not assign operator
    Int c = a; // C // not assign operator
  }
}


int main()
{
  foo();
}

Wednesday, February 23, 2011

Highscore - The Boost C++ Libraries

There are so many articles and exmaples for boost.



How to build boost library as release & static & multi thread & static runtime library in visual studio

In boost directory, type the followng command.

bjam variant=release link=static threading=multi address-model=32 runtime-link=static

It may take a few minutes to build libraries, and you will get "*-mt-s-*.lib" files in library directory($root/stage/lib).

Form example,
2011-02-23 오후 11:53 720,852 libboost_date_time-vc80-mt-s-1_45.lib
2011-02-23 오후 11:54 1,893,972 libboost_filesystem-vc80-mt-s-1_45.lib
2011-02-23 오후 11:55 5,675,002 libboost_graph-vc80-mt-s-1_45.lib
2011-02-23 오후 11:48 423,886 libboost_iostreams-vc80-mt-s-1_45.lib
2011-02-23 오후 11:50 1,550,020 libboost_math_c99-vc80-mt-s-1_45.lib
2011-02-23 오후 11:50 1,570,926 libboost_math_c99f-vc80-mt-s-1_45.lib
2011-02-23 오후 11:50 1,538,966 libboost_math_c99l-vc80-mt-s-1_45.lib
2011-02-23 오후 11:49 5,769,460 libboost_math_tr1-vc80-mt-s-1_45.lib
2011-02-23 오후 11:49 5,905,880 libboost_math_tr1f-vc80-mt-s-1_45.lib
2011-02-23 오후 11:49 5,706,556 libboost_math_tr1l-vc80-mt-s-1_45.lib
2011-02-23 오후 11:51 320,242 libboost_prg_exec_monitor-vc80-mt-s-1_45.lib
2011-02-23 오후 11:50 5,050,400 libboost_program_options-vc80-mt-s-1_45.lib
2011-02-23 오후 11:50 108,922 libboost_random-vc80-mt-s-1_45.lib
2011-02-23 오후 11:54 10,374,684 libboost_regex-vc80-mt-s-1_45.lib
2011-02-23 오후 11:51 11,429,740 libboost_serialization-vc80-mt-s-1_45.lib
2011-02-23 오후 11:51 926,442 libboost_signals-vc80-mt-s-1_45.lib
2011-02-23 오후 11:53 92,208 libboost_system-vc80-mt-s-1_45.lib
2011-02-23 오후 11:52 10,232,224 libboost_test_exec_monitor-vc80-mt-s-1_45.lib
2011-02-23 오후 11:52 464,268 libboost_thread-vc80-mt-s-1_45.lib
2011-02-23 오후 11:52 11,447,096 libboost_unit_test_framework-vc80-mt-s-1_45.lib
2011-02-23 오후 11:53 46,877,338 libboost_wave-vc80-mt-s-1_45.lib
2011-02-23 오후 11:51 9,099,918 libboost_wserialization-vc80-mt-s-1_45.lib