Debian Codegraph @

gcc/testsuite/g++.dg/expr/anew3.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 X
13 {
14 int a;
15 double b;
16 };
17
18 X* allocate(int n)
19 {
20 void *p;
21 p = malloc(n * sizeof (X));
22 memset (p, 0xff, n * sizeof(X));
23 return new (p) X[n]();
24 }
25
26 int main()
27 {
28 const int n = 17;
29 X* p = allocate(n);
30 for (int i = 0; i < n; ++i)
31 if (p[i].a != 0 || p[i].b != 0.0)
32 abort ();
33 exit (0);
34 }