Answer the question
In order to leave comments, you need to log in
How to provoke bad_alloc?
There is such a piece of code. How to provoke bad_alloc?
type_T * new_value;
try {
new_value = new type_T;
}
catch (std::bad_alloc& ba){
std::cerr « "bad_alloc caught: " « ba.what() « std::endl;
return false;
}
Answer the question
In order to leave comments, you need to log in
If you have a class, then let it try to grab a lot of memory. I did the following manipulations with your code and it worked.
try {
size_t sz = SIZE_MAX;
auto new_value = new int[sz];
}
catch (std::bad_alloc& ba){
std::cout << "bad_alloc caught: " << ba.what() << std::endl;
return false;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question