ArrayFree
Nó giải phóng bộ đệm của bất kỳ mảng động nào và đặt kích thước của chiều thứ không về 0.
cpp
void ArrayFree(
void& array[] // mảng
);
1
2
3
2
3
Tham số
array[]
[in] Mảng động.
Giá trị trả về
Không có giá trị trả về.
Ghi chú
Việc cần sử dụng hàm ArrayFree()
có thể không xuất hiện quá thường xuyên vì tất cả bộ nhớ đã sử dụng được giải phóng cùng một lúc và công việc chính với các mảng bao gồm việc truy cập vào các bộ đệm chỉ báo. Kích thước của các bộ đệm được hệ thống thực thi của terminal quản lý tự động.
Trong trường hợp cần quản lý bộ nhớ thủ công trong môi trường động phức tạp của ứng dụng, hàm ArrayFree()
cho phép người dùng giải phóng bộ nhớ bị chiếm bởi mảng động không còn cần thiết một cách rõ ràng và ngay lập tức.
Ví dụ:
cpp
#include <Controls\Dialog.mqh>
#include <Controls\Button.mqh>
#include <Controls\Label.mqh>
#include <Controls\ComboBox.mqh>
//--- hằng số định sẵn
#define X_START 0
#define Y_START 0
#define X_SIZE 280
#define Y_SIZE 300
//+------------------------------------------------------------------+
//| Lớp Dialog để làm việc với bộ nhớ |
//+------------------------------------------------------------------+
class CMemoryControl : public CAppDialog
{
private:
//--- kích thước mảng
int m_arr_size;
//--- các mảng
char m_arr_char[];
int m_arr_int[];
float m_arr_float[];
double m_arr_double[];
long m_arr_long[];
//--- nhãn
CLabel m_lbl_memory_physical;
CLabel m_lbl_memory_total;
CLabel m_lbl_memory_available;
CLabel m_lbl_memory_used;
CLabel m_lbl_array_size;
CLabel m_lbl_array_type;
CLabel m_lbl_error;
CLabel m_lbl_change_type;
CLabel m_lbl_add_size;
//--- nút
CButton m_button_add;
CButton m_button_free;
//--- hộp chọn
CComboBox m_combo_box_step;
CComboBox m_combo_box_type;
//--- giá trị hiện tại của loại mảng từ hộp chọn
int m_combo_box_type_value;
public:
CMemoryControl(void);
~CMemoryControl(void);
//--- phương thức tạo đối tượng lớp
virtual bool Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2);
//--- trình xử lý sự kiện biểu đồ
virtual bool OnEvent(const int id,const long &lparam,const double &dparam,const string &sparam);
protected:
//--- tạo nhãn
bool CreateLabel(CLabel &lbl,const string name,const int x,const int y,const string str,const int font_size,const int clr);
//--- tạo các phần tử điều khiển
bool CreateButton(CButton &button,const string name,const int x,const int y,const string str,const int font_size,const int clr);
bool CreateComboBoxStep(void);
bool CreateComboBoxType(void);
//--- trình xử lý sự kiện
void OnClickButtonAdd(void);
void OnClickButtonFree(void);
void OnChangeComboBoxType(void);
//--- phương thức làm việc với mảng hiện tại
void CurrentArrayFree(void);
bool CurrentArrayAdd(void);
};
//+------------------------------------------------------------------+
//| Giải phóng bộ nhớ của mảng hiện tại |
//+------------------------------------------------------------------+
void CMemoryControl::CurrentArrayFree(void)
{
//--- đặt lại kích thước mảng
m_arr_size=0;
//--- giải phóng mảng
if(m_combo_box_type_value==0)
ArrayFree(m_arr_char);
if(m_combo_box_type_value==1)
ArrayFree(m_arr_int);
if(m_combo_box_type_value==2)
ArrayFree(m_arr_float);
if(m_combo_box_type_value==3)
ArrayFree(m_arr_double);
if(m_combo_box_type_value==4)
ArrayFree(m_arr_long);
}
//+------------------------------------------------------------------+
//| Thử thêm bộ nhớ cho mảng hiện tại |
//+------------------------------------------------------------------+
bool CMemoryControl::CurrentArrayAdd(void)
{
//--- thoát nếu kích thước bộ nhớ đã sử dụng vượt quá kích thước bộ nhớ vật lý
if(TerminalInfoInteger(TERMINAL_MEMORY_PHYSICAL)/TerminalInfoInteger(TERMINAL_MEMORY_USED)<2)
return(false);
//--- thử phân bổ bộ nhớ theo loại hiện tại
if(m_combo_box_type_value==0 && ArrayResize(m_arr_char,m_arr_size)==-1)
return(false);
if(m_combo_box_type_value==1 && ArrayResize(m_arr_int,m_arr_size)==-1)
return(false);
if(m_combo_box_type_value==2 && ArrayResize(m_arr_float,m_arr_size)==-1)
return(false);
if(m_combo_box_type_value==3 && ArrayResize(m_arr_double,m_arr_size)==-1)
return(false);
if(m_combo_box_type_value==4 && ArrayResize(m_arr_long,m_arr_size)==-1)
return(false);
//--- bộ nhớ đã được phân bổ
return(true);
}
//+------------------------------------------------------------------+
//| Xử lý sự kiện |
//+------------------------------------------------------------------+
EVENT_MAP_BEGIN(CMemoryControl)
ON_EVENT(ON_CLICK,m_button_add,OnClickButtonAdd)
ON_EVENT(ON_CLICK,m_button_free,OnClickButtonFree)
ON_EVENT(ON_CHANGE,m_combo_box_type,OnChangeComboBoxType)
EVENT_MAP_END(CAppDialog)
//+------------------------------------------------------------------+
//| Hàm tạo |
//+------------------------------------------------------------------+
CMemoryControl::CMemoryControl(void)
{
}
//+------------------------------------------------------------------+
//| Hàm hủy |
//+------------------------------------------------------------------+
CMemoryControl::~CMemoryControl(void)
{
}
//+------------------------------------------------------------------+
//| Phương thức tạo đối tượng lớp |
//+------------------------------------------------------------------+
bool CMemoryControl::Create(const long chart,const string name,const int subwin,
const int x1,const int y1,const int x2,const int y2)
{
//--- tạo đối tượng lớp cơ sở
if(!CAppDialog::Create(chart,name,subwin,x1,y1,x2,y2))
return(false);
//--- chuẩn bị chuỗi cho nhãn
string str_physical="Memory physical = "+(string)TerminalInfoInteger(TERMINAL_MEMORY_PHYSICAL)+" Mb";
string str_total="Memory total = "+(string)TerminalInfoInteger(TERMINAL_MEMORY_TOTAL)+" Mb";
string str_available="Memory available = "+(string)TerminalInfoInteger(TERMINAL_MEMORY_AVAILABLE)+" Mb";
string str_used="Memory used = "+(string)TerminalInfoInteger(TERMINAL_MEMORY_USED)+" Mb";
//--- tạo nhãn
if(!CreateLabel(m_lbl_memory_physical,"physical_label",X_START+10,Y_START+5,str_physical,12,clrBlack))
return(false);
if(!CreateLabel(m_lbl_memory_total,"total_label",X_START+10,Y_START+30,str_total,12,clrBlack))
return(false);
if(!CreateLabel(m_lbl_memory_available,"available_label",X_START+10,Y_START+55,str_available,12,clrBlack))
return(false);
if(!CreateLabel(m_lbl_memory_used,"used_label",X_START+10,Y_START+80,str_used,12,clrBlack))
return(false);
if(!CreateLabel(m_lbl_array_type,"type_label",X_START+10,Y_START+105,"Array type = double",12,clrBlack))
return(false);
if(!CreateLabel(m_lbl_array_size,"size_label",X_START+10,Y_START+130,"Array size = 0",12,clrBlack))
return(false);
if(!CreateLabel(m_lbl_error,"error_label",X_START+10,Y_START+155,"",12,clrRed))
return(false);
if(!CreateLabel(m_lbl_change_type,"change_type_label",X_START+10,Y_START+185,"Change type",10,clrBlack))
return(false);
if(!CreateLabel(m_lbl_add_size,"add_size_label",X_START+10,Y_START+210,"Add to array",10,clrBlack))
return(false);
//--- tạo các phần tử điều khiển
if(!CreateButton(m_button_add,"add_button",X_START+15,Y_START+245,"Add",12,clrBlue))
return(false);
if(!CreateButton(m_button_free,"free_button",X_START+75,Y_START+245,"Free",12,clrBlue))
return(false);
if(!CreateComboBoxType())
return(false);
if(!CreateComboBoxStep())
return(false);
//--- khởi tạo biến
m_arr_size=0;
//--- thực thi thành công
return(true);
}
//+------------------------------------------------------------------+
//| Tạo nút |
//+------------------------------------------------------------------+
bool CMemoryControl::CreateButton(CButton &button,const string name,const int x,
const int y,const string str,const int font_size,
const int clr)
{
//--- tạo nút
if(!button.Create(m_chart_id,name,m_subwin,x,y,x+50,y+20))
return(false);
//--- văn bản
if(!button.Text(str))
return(false);
//--- kích thước phông chữ
if(!button.FontSize(font_size))
return(false);
//--- màu nhãn
if(!button.Color(clr))
return(false);
//--- thêm nút vào các phần tử điều khiển
if(!Add(button))
return(false);
//--- thực thi thành công
return(true);
}
//+------------------------------------------------------------------+
//| Tạo hộp chọn cho kích thước mảng |
//+------------------------------------------------------------------+
bool CMemoryControl::CreateComboBoxStep(void)
{
//--- tạo hộp chọn
if(!m_combo_box_step.Create(m_chart_id,"step_combobox",m_subwin,X_START+100,Y_START+185,X_START+200,Y_START+205))
return(false);
//--- thêm phần tử vào hộp chọn
if(!m_combo_box_step.ItemAdd("100 000",100000))
return(false);
if(!m_combo_box_step.ItemAdd("1 000 000",1000000))
return(false);
if(!m_combo_box_step.ItemAdd("10 000 000",10000000))
return(false);
if(!m_combo_box_step.ItemAdd("100 000 000",100000000))
return(false);
//--- đặt phần tử hiện tại của hộp chọn
if(!m_combo_box_step.SelectByValue(1000000))
return(false);
//--- thêm hộp chọn vào các phần tử điều khiển
if(!Add(m_combo_box_step))
return(false);
//--- thực thi thành công
return(true);
}
//+------------------------------------------------------------------+
//| Tạo hộp chọn cho loại mảng |
//+------------------------------------------------------------------+
bool CMemoryControl::CreateComboBoxType(void)
{
//--- tạo hộp chọn
if(!m_combo_box_type.Create(m_chart_id,"type_combobox",m_subwin,X_START+100,Y_START+210,X_START+200,Y_START+230))
return(false);
//--- thêm phần tử vào hộp chọn
if(!m_combo_box_type.ItemAdd("char",0))
return(false);
if(!m_combo_box_type.ItemAdd("int",1))
return(false);
if(!m_combo_box_type.ItemAdd("float",2))
return(false);
if(!m_combo_box_type.ItemAdd("double",3))
return(false);
if(!m_combo_box_type.ItemAdd("long",4))
return(false);
//--- đặt phần tử hiện tại của hộp chọn
if(!m_combo_box_type.SelectByValue(3))
return(false);
//--- lưu phần tử hiện tại của hộp chọn
m_combo_box_type_value=3;
//--- thêm hộp chọn vào các phần tử điều khiển
if(!Add(m_combo_box_type))
return(false);
//--- thực thi thành công
return(true);
}
//+------------------------------------------------------------------+
//| Tạo nhãn |
//+------------------------------------------------------------------+
bool CMemoryControl::CreateLabel(CLabel &lbl,const string name,const int x,
const int y,const string str,const int font_size,
const int clr)
{
//--- tạo nhãn
if(!lbl.Create(m_chart_id,name,m_subwin,x,y,0,0))
return(false);
//--- văn bản
if(!lbl.Text(str))
return(false);
//--- kích thước phông chữ
if(!lbl.FontSize(font_size))
return(false);
//--- màu
if(!lbl.Color(clr))
return(false);
//--- thêm nhãn vào các phần tử điều khiển
if(!Add(lbl))
return(false);
//--- thành công
return(true);
}
//+------------------------------------------------------------------+
//| Trình xử lý sự kiện nhấn nút "Add" |
//+------------------------------------------------------------------+
void CMemoryControl::OnClickButtonAdd(void)
{
//--- tăng kích thước mảng
m_arr_size+=(int)m_combo_box_step.Value();
//--- thử phân bổ bộ nhớ cho mảng hiện tại
if(CurrentArrayAdd())
{
//--- bộ nhớ đã được phân bổ, hiển thị trạng thái hiện tại trên màn hình
m_lbl_memory_available.Text("Memory available = "+(string)TerminalInfoInteger(TERMINAL_MEMORY_AVAILABLE)+" Mb");
m_lbl_memory_used.Text("Memory used = "+(string)TerminalInfoInteger(TERMINAL_MEMORY_USED)+" Mb");
m_lbl_array_size.Text("Array size = "+IntegerToString(m_arr_size));
m_lbl_error.Text("");
}
else
{
//--- không thể phân bổ bộ nhớ, hiển thị thông báo lỗi
m_lbl_error.Text("Array is too large, error!");
//--- trả về kích thước mảng trước đó
m_arr_size-=(int)m_combo_box_step.Value();
}
}
//+------------------------------------------------------------------+
//| Trình xử lý sự kiện nhấn nút "Free" |
//+------------------------------------------------------------------+
void CMemoryControl::OnClickButtonFree(void)
{
//--- giải phóng bộ nhớ của mảng hiện tại
CurrentArrayFree();
//--- hiển thị trạng thái hiện tại trên màn hình
m_lbl_memory_available.Text("Memory available = "+(string)TerminalInfoInteger(TERMINAL_MEMORY_AVAILABLE)+" Mb");
m_lbl_memory_used.Text("Memory used = "+(string)TerminalInfoInteger(TERMINAL_MEMORY_USED)+" Mb");
m_lbl_array_size.Text("Array size = 0");
m_lbl_error.Text("");
}
//+------------------------------------------------------------------+
//| Trình xử lý sự kiện thay đổi hộp chọn |
//+------------------------------------------------------------------+
void CMemoryControl::OnChangeComboBoxType(void)
{
//--- kiểm tra xem loại mảng có thay đổi không
if(m_combo_box_type.Value()!=m_combo_box_type_value)
{
//--- giải phóng bộ nhớ của mảng hiện tại
OnClickButtonFree();
//--- làm việc với loại mảng khác
m_combo_box_type_value=(int)m_combo_box_type.Value();
//--- hiển thị loại mảng mới trên màn hình
if(m_combo_box_type_value==0)
m_lbl_array_type.Text("Array type = char");
if(m_combo_box_type_value==1)
m_lbl_array_type.Text("Array type = int");
if(m_combo_box_type_value==2)
m_lbl_array_type.Text("Array type = float");
if(m_combo_box_type_value==3)
m_lbl_array_type.Text("Array type = double");
if(m_combo_box_type_value==4)
m_lbl_array_type.Text("Array type = long");
}
}
//--- đối tượng lớp CMemoryControl
CMemoryControl ExtDialog;
//+------------------------------------------------------------------+
//| Hàm khởi tạo chuyên gia |
//+------------------------------------------------------------------+
int OnInit()
{
//--- tạo hộp thoại
if(!ExtDialog.Create(0,"MemoryControl",0,X_START,Y_START,X_SIZE,Y_SIZE))
return(INIT_FAILED);
//--- khởi chạy
ExtDialog.Run();
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Hàm hủy chuyên gia |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
ExtDialog.Destroy(reason);
}
//+------------------------------------------------------------------+
//| Hàm sự kiện biểu đồ chuyên gia |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
ExtDialog.ChartEvent(id,lparam,dparam,sparam);
}
1
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374