• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // operator new(unsigned)
2 // operator new[](unsigned)
3 // operator new(unsigned, std::nothrow_t const&)
4 // operator new[](unsigned, std::nothrow_t const&)
5 
6 #include <stdlib.h>
7 
8 #include <new>
9 
10 using std::nothrow_t;
11 
12 // A big structure.  Its details don't matter.
13 typedef struct {
14            int array[1000];
15         } s;
16 
operator new(std::size_t n)17 __attribute__((noinline)) void* operator new (std::size_t n) throw (std::bad_alloc)
18 {
19     return malloc(n);
20 }
21 
operator new(std::size_t n,std::nothrow_t const &)22 __attribute__((noinline)) void* operator new (std::size_t n, std::nothrow_t const &) throw ()
23 {
24     return malloc(n);
25 }
26 
operator new[](std::size_t n)27 __attribute__((noinline)) void* operator new[] (std::size_t n) throw (std::bad_alloc)
28 {
29     return malloc(n);
30 }
31 
operator new[](std::size_t n,std::nothrow_t const &)32 __attribute__((noinline)) void* operator new[] (std::size_t n, std::nothrow_t const &) throw ()
33 {
34     return malloc(n);
35 }
36 
operator delete(void * p)37 __attribute__((noinline)) void operator delete (void* p) throw()
38 {
39     return free(p);
40 }
41 
operator delete[](void * p)42 __attribute__((noinline)) void operator delete[] (void* p) throw()
43 {
44     return free(p);
45 }
46 
main(void)47 int main(void)
48 {
49     s*        p1 = new                s;
50     s*        p2 = new (std::nothrow) s;
51     char*     c1 = new                char[2000];
52     char*     c2 = new (std::nothrow) char[2000];
53     delete p1;
54     delete p2;
55     delete [] c1;
56     delete [] c2;
57     return 0;
58 }
59 
60 
61