1
2
3
4
#include<cstdio>
5
#include<iostream>
6
#include<cstdlib>
7
#include<typeinfo>
8
9
using namespace std;
10
11
#define SIZE 8
12
#define SEC (60*60UL)
13
14
//1. test mem alignment
15
#pragma pack(0)
16
17
void fn1()
18
{
19
cout<<endl<<"next!"<<endl;
20
21
cout<<"---------------------"<<endl;
22
23
int a;
24
char b;
25
int c;
26
cout<<&a<<endl;
27
cout<<&b<<endl;
28
printf("0x%08x ", &b);
29
cout<<&c<<endl;
30
31
// int ss[20] = "33424";
32
}
33
//2.使用調(diào)用函數(shù)寫入數(shù)據(jù),而不是讀出
34
int * m_uiROIID = new int[SIZE];
35
36
int* getROIID ()
37
{
38
return m_uiROIID;
39
}
40
41
void prt()
42
{
43
for (int i = 0;i < 3;i++)
44
scanf("%d", getROIID()+i);
45
46
for (int i = 0;i < SIZE;i++)
47
{
48
printf("%d ,", m_uiROIID[i]);
49
}
50
}
51
//3. a hash function
52
long hashx(long x)
53
{
54
long a = x | 0x0000FFFF;
55
long b = x >> 16;
56
return (a & ~b) | ( b & ~a);
57
}
58
59
//還不是很正確的
60
void test()
61
{
62
for (long i = 0; i < 5;i++)
63
printf(" %f\n", hashx(i) );
64
}
65
66
//4.引用指針用給參數(shù)分配空間
67
void create(char *& p)
68
{
69
p = new char;
70
p = "good idear";
71
}
72
void destroy(char *&p)
73
{
74
delete p;
75
}
76
void testRefPoint()
77
{
78
char* p = NULL;
79
if ( p == NULL) printf("%s", p);
80
else printf("not NULL \n");
81
create(p);
82
printf("%s", p);
83
if ( p == NULL) printf("NULL");
84
else printf("not NULL after create");
85
86
delete p;
87
printf("%s", p);
88
if ( p == NULL) printf("NULL");
89
else printf("not NULL after create");
90
printf("\n");
91
}
92
//5.指針類型決定加1操作的編移量
93
void testPointer()
94
{
95
int a[5] = {1,2,3,4,5};
96
int *ptr = (int*)(&a+1);
97
printf("%d %d" , *(a+1), *(ptr-1) );
98
99
printf("\n");
100
}
101
102
// 6.數(shù)據(jù)是傳遞指針,測試類型同5
103
void foo(int [][3] );
104
void main1()
105
{
106
int a [3][3]= { { 1,2,3} , { 4,5,6},{7,8,9}};
107
108
109
printf("%d" , a[2][1]);
110
foo(a);
111
printf("%d" , a[2][1]);
112
113
printf("\n");
114
}
115
void foo( int b[][3])
116
{
117
++ b;
118
b[1][1] =9;
119
}
120
// 7.逗號(hào)表達(dá)式和指針使用
121
void testP()
122
{
123
int a, b,c, d;
124
a=3;
125
b=5;
126
c=a,b;
127
d=(a,b);
128
printf("c=%d" ,c);
129
printf("d=%d" ,d);
130
131
int arr[]={6,7,8,9,10};
132
133
int *ptr=arr;
134
135
*(ptr++)+=123;
136
137
printf("%d,%d",*ptr,*(++ptr));//它是從的計(jì)算的
138
139
140
}
141
//8。CPP的相等比較
142
void testCStr()
143
{
144
char str1[] = "abc";
145
char str2[] = "abc";
146
const char str3[] = "abc";
147
const char str4[] = "abc";
148
const char* str5 = "abc";
149
const char* str6 = "abc";
150
cout << ( str1==str2 ) << endl; // 輸出什么?
151
cout << ( str3==str4 ) << endl; // 輸出什么?
152
cout << ( str5==str6 ) << endl; // 輸出什么?
153
}
154
155
//10.虛析構(gòu)函數(shù), 變量定義出現(xiàn),typeid和type_info的使用
156
class Base
157
{
158
static const int Size = 9;
159
public:
160
Base()
161
{
162
cout<<"Base()'s ctor"<<endl;
163
164
165
}
166
virtual ~Base()=0;
167
168
};
169
inline Base::~Base()
170
{
171
cout<<" Base's dtor"<<endl;
172
}
173
174
class Derived:public Base
175
{
176
public:
177
Derived()
178
{
179
cout<<"int Derived ctor"<<endl;
180
}
181
~Derived()
182
{
183
cout<<"int Derived dtor"<<endl;
184
}
185
};
186
187
//11.判斷是c不是cpp程序, atexit 在main后調(diào)用函數(shù)
188
void testCOrCpp()
189
{
190
cout<<"is cpp"<<__cplusplus<<endl;
191
// cout<<_STDC_<<endl;
192
193
atexit(fn1);
194
}
195
196
//12. test the class ctor and dtor
197
//Base a;
198
void testCtor()
199
{
200
Base* pBase = new Derived;
201
// Derived dObj;
202
// pBase = &dObj;
203
204
// delete pBase;
205
206
cout<<" ---------------before delete pBase--------"<<endl;
207
208
printf("******-----> %f", 5.);
209
210
printf("------> %d\n", 5);
211
212
char *p = "what";
213
p = "abc";
214
cout<<p<<endl;
215
216
if( typeid(*pBase) == typeid(Base) )
217
cout<<" typeid Base"<<endl;
218
else if( typeid( *pBase) == typeid(Derived))
219
cout<<" typeid Derived"<<endl;
220
else
221
cout<<" typeid NULL"<<endl;
222
223
const type_info& ti = typeid(*pBase);
224
225
cout<<"ti.name()= " << ti.name()<<endl;
226
227
cout<<"???????"<< typeid(*pBase).name()<<endl;
228
229
}
230
231
232
int main()
233
{
234
int a[SIZE];
235
236
cout<<SEC<<endl;
237
// prt();
238
// testCOrCpp();
239
240
// testCStr();
241
242
// testCtor();
243
244
testRefPoint(); /*
245
testPointer();
246
247
main1();
248
testP();
249
250
int *pA = new int;
251
252
delete pA;
253
254
*pA = 3;
255
*/
256
257
// test();
258
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258
