Debian Codegraph @

gcc/testsuite/g++.dg/expr/anew4.C

raw
1 // { dg-do run }
2 // { dg-skip-if "requires hosted libstdc++ for stdlib malloc" { ! hostedlib } }
3
4 // PR 11228: array operator new, with zero-initialization and a variable sized array.
5 // Regression test for PR
6 // Author: Matt Austern <[email protected]>
7
8 #include <new>
9 #include <stdlib.h>
10 #include <string.h>
11
12 struct B
13 {
14 B();
15 int n;
16 };
17
18 B::B()
19 {
20 n = 137;
21 }
22
23
24 struct D : public B
25 {
26 double x;
27 };
28
29
30 D* allocate(int n)
31 {
32 void *p;
33 p = malloc(n * sizeof (D));
34 memset (p, 0xff, n * sizeof(D));
35 return new (p) D[n]();
36 }
37
38 int main()
39 {
40 const int n = 17;
41 D* p = allocate(n);
42 for (int i = 0; i < n; ++i)
43 if (p[i].n != 137 || p[i].x != 0)
44 abort ();
45 exit (0);
46 }