Hello everyone,
Please help to comment whether my following understanding is correct,
1. whether or not we are using auto_ptr to allocate new object on heap (using new), there may be bad_alloc exceptions;
2. when we met with such exceptions, we catch it (bad_alloc) and try to mininize the operation in catch handler block (since when bad_alloc occurs, it means memory is running out, we can not do anything complex in handler).
Both are correct? Please feel free to correct me if I am wrong.
thanks in advance,
George
4 1146
The C++ language spec says that a bad_alloc exceprtion will be thrown when new runs out of heap memory.
That said, this does not apply to Windows, which has a virtual memory manager. When heap memory runs lows, new just calls the virtual memory manager for another page of memory. When the memory is completely allocated, swapping occurs with the hard disc. This will continue until your hard disc fills up. In this condition, your response is very, very slow as your machine vibrates across the floor swapping memory pages with the hard disc. That would be your signal that there is a problem. For that reason, I never use a catch block with new.
I don't know the situation with the various Unix/Linux variants.
The bad_alloc is supposed to be thrown when there is just enough heap left to do the throw. Therefore, you should not allocate heap memory in your bad_alloc handler. If you do, you could cause the exception-inside-the-catch-block situation which is an automatic crash.
Thanks for your valuable advice, weaknessforcats!
Do you think we need to call delete or delete[] to free memory if our handler for bad_alloc?
The C++ language spec says that a bad_alloc exceprtion will be thrown when new runs out of heap memory.
That said, this does not apply to Windows, which has a virtual memory manager. When heap memory runs lows, new just calls the virtual memory manager for another page of memory. When the memory is completely allocated, swapping occurs with the hard disc. This will continue until your hard disc fills up. In this condition, your response is very, very slow as your machine vibrates across the floor swapping memory pages with the hard disc. That would be your signal that there is a problem. For that reason, I never use a catch block with new.
I don't know the situation with the various Unix/Linux variants.
The bad_alloc is supposed to be thrown when there is just enough heap left to do the throw. Therefore, you should not allocate heap memory in your bad_alloc handler. If you do, you could cause the exception-inside-the-catch-block situation which is an automatic crash.
regards,
George
You don't delete in a bad_alloc handler because you got there due to a failure to allocate memory. A delete would probably crash your program.
Thanks weaknessforcats,
My question is answered.
You don't delete in a bad_alloc handler because you got there due to a failure to allocate memory. A delete would probably crash your program.
regards,
George
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Asfand Yar Qazi |
last post by:
Hi,
Just wondering, if I do a:
std::set_new_handler(std::terminate)
I won't have to worry about a std::bad_alloc being thrown when I do a
'new ...' or a 'new(nothrow) ...', right?
Its...
|
by: vulcan.wayne |
last post by:
Hi
Shouldn't this code cause a core dump when the sys runs out of mem,
when there's no default exception handling mechanism.
void fn()
{
SomeObj *so = NULL;
so = new SomeObj; // -> no...
|
by: Gary Wessle |
last post by:
Hi
I am getting this error when running a very similar code like the
below, it is made for illustration only.
thanks
***************************************
**************** error...
|
by: Mohsen |
last post by:
Hello everyone,
In my program, I have to define many pointers and when I want to
compile my program (in UNIX), it gives me the following error:
terminate called after throwing an instance of...
|
by: schizoid_man |
last post by:
Hi,
I have the following code snippets and I get a std::bad_alloc error
where I think there should be none. I've attached the relevant bits of
the base class, derived class and the .cpp file...
|
by: Alerion |
last post by:
Hello everyone,
I've been a regular of this forum but this is my first post, generally I can find the answer to my question already, but this time I'm having a somewhat specific problem.
For...
|
by: simbasaurus |
last post by:
Hello!
I am trying to make sure that I clean up and free all the resources that I allocate in my code.
In the following code, I am assuming that if new throws bad_alloc, than the value of...
|
by: George2 |
last post by:
Hello everyone,
Please help to comment whether my following understanding is correct,
1. whether or not we are using auto_ptr to allocate new object on heap
(using new), there may be...
|
by: George2 |
last post by:
Hello everyone,
I usually check whether there is bad_alloc thrown to identify whether the allocation is success or not.
My question is,
Is there a way to disable bad_alloc and just to...
|
by: arjor |
last post by:
hello guys
i get the following warning when i try to try an throw an exception
given by the new command.
declaration:
void CopyTree(Vertex*& copy, const Vertex* originalTree)...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |