1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // test operator new [] nothrow by replacing only operator new 11 12 #include <new> 13 #include <cstddef> 14 #include <cstdlib> 15 #include <cassert> 16 #include <limits> 17 18 int new_called = 0; 19 20 void* operator new(std::size_t s) throw(std::bad_alloc) 21 { 22 ++new_called; 23 return std::malloc(s); 24 } 25 26 void operator delete(void* p) throw() 27 { 28 --new_called; 29 std::free(p); 30 } 31 32 volatile int A_constructed = 0; 33 34 struct A 35 { 36 A() {++A_constructed;} 37 ~A() {--A_constructed;} 38 }; 39 40 int main() 41 { 42 A* ap = new (std::nothrow) A[3]; 43 assert(ap); 44 assert(A_constructed == 3); 45 assert(new_called); 46 delete [] ap; 47 assert(A_constructed == 0); 48 assert(!new_called); 49 } 50