language: C++ 4.7.2 (gcc-4.7.2)
date: 515 days 23 hours ago
link:
visibility: public
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <cstdlib>
 
struct dummy {
  dummy() : m_t( 23) {}
  ~dummy() {}
  int m_t;
  void * operator new( size_t size ) {
    std::cout << "new: Requested " << size << " bytes" << std::endl;
    void * v = malloc( size );
    std::cout << "Object allocated at 0x" << std::hex << reinterpret_cast<long int>( v ) << std::endl;
    return v;
  }
  
  void * operator new[]( size_t size ) {
    std::cout << "new[]: Requested " << size << " bytes" << std::endl;
    void * v = malloc( size );
    std::cout << "Object Array allocated at 0x" << std::hex << reinterpret_cast<long int>( v ) << std::endl;
    return v;
  }
  
  void operator delete( void * v) {
    std::cout << "delete: at 0x" << std::hex << reinterpret_cast<long int>( v ) << std::endl;
    //free( v ); // We leak instead of crashing
  }
  
  void operator delete[]( void * v) {
    std::cout << "delete[]: at 0x" << std::hex << reinterpret_cast<long int>( v ) << std::endl;
    //free( v ); // We leak instead of crashing
  }
  
  
};
 
int main( ) {
  dummy * d1 = new dummy();
  std::cout << "d1 at 0x" << std::hex << reinterpret_cast<long int>( d1 ) << std::endl;
  delete d1;
  dummy * d2 = new dummy[ 1 ];
  std::cout << "d2 at 0x" << std::hex << reinterpret_cast<long int>( d2 ) << std::endl; 
  delete d2;
  delete[] d2;
  return 0;
}