| 1 |
// { dg-do run } |
| 2 |
// GROUPS passed operator-new |
| 3 |
// Check that if there is a user defined class-specific operator |
| 4 |
// new for a given class, that operator is invoked when a new |
| 5 |
// object of the class is requested, regardless of whether or not |
| 6 |
// there is also a constructor for the given class, and regardless |
| 7 |
// of whether or not the constructor for the given class is defined |
| 8 |
// before or after the new operator is even declared. |
| 9 |
|
| 10 |
extern "C" int printf (const char *, ...); |
| 11 |
|
| 12 |
typedef __SIZE_TYPE__ size_t; |
| 13 |
|
| 14 |
struct base { |
| 15 |
int i; |
| 16 |
|
| 17 |
base () |
| 18 |
{ |
| 19 |
i = 7; |
| 20 |
} |
| 21 |
|
| 22 |
void * operator new (size_t size); |
| 23 |
void operator delete (void*); |
| 24 |
}; |
| 25 |
|
| 26 |
class derived : public base { |
| 27 |
int j; |
| 28 |
}; |
| 29 |
|
| 30 |
int new_call_count = 0; |
| 31 |
int expected_size = 0; |
| 32 |
int errors = 0; |
| 33 |
|
| 34 |
int main () |
| 35 |
{ |
| 36 |
base* base_ptr; |
| 37 |
derived* derived_ptr; |
| 38 |
|
| 39 |
expected_size = sizeof (int); |
| 40 |
base_ptr = new base; |
| 41 |
expected_size = 2 * sizeof (int); |
| 42 |
derived_ptr = new derived (); |
| 43 |
|
| 44 |
if ((new_call_count != 2) || (errors != 0)) |
| 45 |
{ printf ("FAIL\n"); return 1; } |
| 46 |
else |
| 47 |
printf ("PASS\n"); |
| 48 |
|
| 49 |
return 0; |
| 50 |
} |
| 51 |
|
| 52 |
int allocation_space[100]; |
| 53 |
int* allocation_ptr = allocation_space; |
| 54 |
|
| 55 |
void base::operator delete (void* p) |
| 56 |
{ |
| 57 |
} |
| 58 |
|
| 59 |
void *base::operator new (size_t size) |
| 60 |
{ |
| 61 |
int* return_value = allocation_ptr; |
| 62 |
|
| 63 |
new_call_count++; |
| 64 |
if (size != expected_size) |
| 65 |
errors++; |
| 66 |
allocation_ptr += (size + sizeof(int) - 1) / sizeof(int); |
| 67 |
return (void*) return_value; |
| 68 |
} |