* Câu hỏi : làm sao để set và reset trạng thái của kiểu ouput thông qua hàm flags ?
answer :
#include <iostream>
int main(){
int integer_value = 1000;
double double_value = 0.334343;
//display flags value in original format
std
::cout << "-- Original format --\n"; std
::cout << "The value of flags variable is : " << std
::cout.
flags() << "\n"; std
::cout << "integer value " << integer_value
<< "\n"; std
::cout << "double value " << double_value
<< "\n";
//use cout flags function to save original format
std
::ios_base::fmtflags original_format
= std
::cout.
flags(); //change format
std
::cout << std
::showbase << std
::oct << std
::scientific << "\n";
std
::cout << "-- New format --\n"; std
::cout << "The value of flags variable is : " << std
::cout.
flags() << "\n"; std
::cout << "integer value " << integer_value
<< "\n"; std
::cout << "double value " << double_value
<< "\n";
//now, restore format
std
::cout.
flags(original_format
); std
::cout << "-- Restore --\n"; std
::cout << "The value of flags variable is : " << std
::cout.
flags() << "\n"; std
::cout << "integer value " << integer_value
<< "\n"; std
::cout << "double value " << double_value
<< "\n";
return 0;
}
Output :
Code:
-- Original format --
The value of flags variable is : 4098
integer value 1000
double value 0.334343
-- New format --
The value of flags variable is : 011500
integer value 01750
double value 3.343430e-01
-- Restore --
The value of flags variable is : 4098
integer value 1000
double value 0.334343
* Câu hỏi : làm sao in ra kết quả của việc cấp phát lỗi ?
answer :
#include <iostream>
int main(){
double* alloc_arry[5];
for(int x = 0; x < 5; ++x){
alloc_arry[x] = new double[5000000];
if(alloc_arry[x] == 0){
std
::cout << "Fail allocating memory...\n" << x
<< "\n"; break;
}
else{
std
::cout << "Successfully allocating new memory for alloc_arry" << "at " << x << "\n";
}
delete[] alloc_arry[x];
}
return 0;
}
Output :
Code:
Successfully allocating new memory for alloc_arryat 0
Successfully allocating new memory for alloc_arryat 1
Successfully allocating new memory for alloc_arryat 2
Successfully allocating new memory for alloc_arryat 3
Successfully allocating new memory for alloc_arryat 4
* Câu hỏi : làm sao catch fail allocate memory ?
answer :
#include <iostream>
#include <new>
int main(){
double* alloc_arry[5];
try{
for(int x = 0; x < 5; ++x){
alloc_arry[x] = new double[99999999];
std
::cout << "Allocating memory...\n" << x
<< "\n"; }
}
//handle exception
catch(std::bad_alloc &memory_allocation_exception){
std
::cout << "Exception occurs !!" << memory_allocation_exception.what() << "\n";
}
return 0;
}
Output
Code:
Allocating memory...
0
Allocating memory...
1
Allocating memory...
2
Exception occurs !!std::bad_alloc
* Câu hỏi : làm sao để set new handler cho fail allocating memory.
answer :
#include <iostream>
#include <new>
void custom_new_handler(){
std
::cerr << "handle, abort...!"; abort();
}
int main(){
double* alloc_arry[5];
std::set_new_handler(custom_new_handler);
for(int x = 0; x < 5; ++x){
alloc_arry[x] = new double[99999999];
std
::cout << "Allocating memory...\n" << x
<< "\n"; }
return 0;
}
Output :
Code:
Allocating memory...
0
Allocating memory...
1
Allocating memory...
2
handle, abort...!