Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
POD_ZERO_INIT.cpp
Go to the documentation of this file.
1// checking for POD zero initialization
2
3
25#include "config.h"
26
27#ifndef _RWSTD_NO_NEW
28# include <new>
29#elif !defined (_RWSTD_NO_NEW_H)
30# include <new.h>
31#else
32
33# ifndef _RWSTD_SIZE_T
34# define _RWSTD_SIZE_T unsigned long
35# endif
36
37inline void* operator new (_RWSTD_SIZE_T, void *p)
38{
39 return p;
40}
41
42#endif
43
44
45struct A {
46 char m1;
47 short m2;
48 int m3;
49 float m4;
50 double m5;
51 void* m6;
52 char m7 [7];
53 void (A::*m8)();
54};
55
56
57template <class T>
58struct S
59{
61
62 S (T t = T ()): t_ (t) { } // must zero initialize `t' if POD
63};
64
65
66template <class T>
67struct X
68{
70
71 X (): s () { } // must zero initialize `s' if POD
72};
73
74
75int is_zero (const void *pv, unsigned n)
76{
77 const char *pc = (const char*)pv;
78
79 for (unsigned i = 0; i != n; ++i)
80 if (pc [i])
81 return 0;
82
83 return -1;
84}
85
86int is_A_zero (const A &a)
87{
88 return is_zero (&a.m1, sizeof a.m1)
89 && is_zero (&a.m2, sizeof a.m2)
90 && is_zero (&a.m3, sizeof a.m3)
91 && is_zero (&a.m4, sizeof a.m4)
92 && is_zero (&a.m5, sizeof a.m5)
93 && is_zero (&a.m6, sizeof a.m6)
94 && is_zero (&a.m7, sizeof a.m7)
95 && is_zero (&a.m8, sizeof a.m8);
96}
97
98int is_SA_zero (const S<A> &s)
99{
100 return is_A_zero (s.t_);
101}
102
103template <class T>
104int is_X_zero (const X<T> &x)
105{
106 return is_SA_zero (x.s);
107}
108
109void setmem (void *pv, unsigned n, char b)
110{
111 char buf [256];
112
113 char *pc = (char*)pv;
114
115 // set memory to the value of `b'
116 for (unsigned i = 0; i != n; ++i)
117 pc [i] = b;
118
119 // dirty up the stack in the process
120 for (unsigned j = 0; j != sizeof buf; ++j)
121 buf [j] = b;
122}
123
124
125int main ()
126{
127 union {
128 char buf [256];
129 void *pad;
130 } u;
131
132 int result = 0;
133
134 setmem (&u, sizeof u, 'x');
135
136 // check that a fundamental type is zeroed out on initialization
137 int i = int ();
138
139 if (i != 0)
140 result |= 1;
141
142 // check that a POD object is zeroed out on initialization
143 A a = A ();
144
145 if (!is_A_zero (a))
146 result |= 2;
147
148 // check that a POD object is zeroed out on initialization with operator new
149 A *pa = new (&u) A ();
150
151 if (!is_A_zero (*pa))
152 result |= 4;
153
154 setmem (&u, sizeof u, 'x');
155
156 S<A> *ps = new (&u) S<A>();
157
158 if (!is_SA_zero (*ps))
159 result |= 8;
160
161 setmem (&u, sizeof u, 'x');
162
163 X<int> *px = new (&u) X<int>();
164
165 if (!is_X_zero (*px))
166 result |= 16;
167
168 setmem (&u, sizeof u, 'x');
169
170 *px = X<int>();
171
172 if (!is_X_zero (*px))
173 result |= 32;
174
175 return result;
176}
#define i
Definition 17.names.cpp:126
#define T
Definition 17.names.cpp:163
#define t
Definition 17.names.cpp:136
#define u
Definition 17.names.cpp:137
#define x
Definition 17.names.cpp:140
#define a
Definition 17.names.cpp:109
#define A
Definition 17.names.cpp:144
#define buf
Definition 17.names.cpp:452
#define j
Definition 17.names.cpp:127
#define t_
Definition 17.names.cpp:190
#define b
Definition 17.names.cpp:110
#define s
#define n
Definition 17.names.cpp:130
#define p
Definition 17.names.cpp:132
volatile int result
int is_SA_zero(const S< A > &s)
int is_zero(const void *pv, unsigned n)
void setmem(void *pv, unsigned n, char b)
int is_A_zero(const A &a)
int main()
int is_X_zero(const X< T > &x)
int m3
double m5
float m4
void(A::* m8)()
char m1
void * m6
short m2
char m7[7]
S(T t=T())
S< A > s