Sunday, March 6, 2011

How to overload new and delete operator


void* __cdecl operator new(size_t size)
{
 return malloc(size);
}

void __cdecl operator delete(void* p)
{
 free(p);
}

Wednesday, March 2, 2011

Where array count is stored when new[] is called?


[source code]
 
#include <new> // for size_t
#include <stdio.h>

class A 
{
public:
  A() {}
  virtual ~A() {}
  static void* operator new (size_t size)
  {
    void* res = ::operator new(size);
    printf("new   return %p\n", res);
    return res;
  }
 
  static void operator delete (void *p)
  {
    printf("delete       %p\n", p);
    ::operator delete(p);
  }
 
  static void* operator new[](size_t size)
  {
    void* res = ::operator new(size);
    printf("new[] return %p\n", res);
    return res;
  }
 
  static void operator delete[](void *p)
  {
    printf("delete[]     %p\n", p);
    ::operator delete[](p);
  }
};
 
void foo()
{
  A* a = new A;
  printf("a=           %p\n", a);
  delete a;
}
 
void bar()
{
  A* a = new A[5];
  printf("a=           %p\n", a);
  delete []a;
}
 
int main()
{
  foo();
  printf("\n");
  bar();
}
 
 
[result]
 
new   return 00633B88 // same
a=           00633B88 // same
delete       00633B88 // same

new[] return 00631A90
a=           00631A94 // 4 byte skipped !!!
delete[]     00631A90


When new[] is called, you can figure out that array count value(5) is stored prior to new[] operator return pointer value(4 bytes offset in the above result). Of course(as you guess), when delete[] is called, the count value is used to delete all allocated objects.

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();
}