LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-21-2013, 10:31 PM   #1
tfnc99
Member
 
Registered: Sep 2012
Posts: 55

Rep: Reputation: Disabled
Exceptions, try...catch vs std::set_new_handler. C++


So here are 2 code snippets that do essentially the same thing:
Code:
void outOfMemHandler()
{
    std::cerr << "Unable to satisfy request for memory\n";
    std::abort();
}
int main()
{
    std::set_new_handler(outOfMemHandler);
    int *pBigDataArray = new int[100000000L];
    delete [] pBigDataArray;
    return 0;
}
Code:
int main()
{
    try
    {
        int *pBigDataArray = new int[100000000L];
    }
    catch(const std::bad_alloc&)
    {
        std::cerr << "Unable to satisfy request for memory\n";
        std::abort();
    }
    delete [] pBigDataArray;
    return 0;
}
I am very confused as to which one of these is better ( -_- Its programing, there has to be some strange reason one is better than the other). Can someone explain these to me?

Last edited by tfnc99; 03-21-2013 at 10:32 PM.
 
Old 03-22-2013, 04:06 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by tfnc99 View Post
I am very confused ...
If you say so.

Seriously, if you want your application to "abort" at the first sign of allocation-trouble, then who cares. I'd be more worried (concerned) at the application that is so poorly developed that there is ever a concern about whether memory allocation is a concern.

In other words, if your application has exhausted all available memory, what's the next step? Allocate more memory so that you can execute the "special" command to launch a rocket towards the Moon?

Last edited by dwhitney67; 03-22-2013 at 04:08 PM.
 
Old 03-22-2013, 04:18 PM   #3
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,223

Rep: Reputation: 307Reputation: 307Reputation: 307Reputation: 307
In that specific example, where you abort on allocation error, this without exception could be better. Exception means more code, need of enabling it, if not used elsewhere in code, etc. But if you want to do more then just quit your application, then it depends what you want to do.

With "set_new_handler" you can react on allocation errors globally. With exceptions you can react differently on every "new" error, depending on where you write "try" block. Exception also give you opurtunity to skip some code on error (for example this which use allocated memory) and do something else even in another parent function. With "set_new_handler" you can only react on insufficient memory, for example by unallocating some data and then repeat memory allocation or quit. Of course with exception you also can do that, but then need to enclose try-catch in loop. But with exception you can do everything you want if error occur.
 
Old 03-22-2013, 04:50 PM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by eSelix View Post
... But with exception you can do everything you want if error occur.
If, and only if, your system still has memory available for use.
 
Old 03-22-2013, 08:18 PM   #5
tfnc99
Member
 
Registered: Sep 2012
Posts: 55

Original Poster
Rep: Reputation: Disabled
@dwhitney
I have always been told that there really isn't much more to do at that point (bad alloc), besides log something and print an error.

As a side question, can I count on a try..catch loop overriding whatever I have set in a handler, so I can react in special circumstances?

Last edited by tfnc99; 03-22-2013 at 08:22 PM.
 
Old 03-24-2013, 09:15 AM   #6
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,223

Rep: Reputation: 307Reputation: 307Reputation: 307Reputation: 307
Sorry, I do not understand your question, my english is not very good. If you ask about what happen if you use both "set_new_handler" and "try-catch" block, then first is called handler, it can quit application, repeat allocation or throw an exception. If you throw exception then later it can be caught by catch block. By default (when user handler is not set) exception is thrown.

About what to do when you have no free memory, quiting application is a last resort. Depending what you can do, you should free some memory, show message with information what happend and question what to do with user data, save or not, or if your program just executed long task (ex. brute-force cracking password), store current state to resume it later, for example after system reboot or on another machine. If error occured in some program feature you can just end executing of this one module and not whole program. For example: user invoked search feature in editor application on big text file, then it should get information about not possibility to do search and resume normal editing mode.
 
1 members found this post helpful.
  


Reply

Tags
c++, exception, memory


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printf vs std::cout and std::ofstream grob115 Programming 2 01-16-2013 11:49 AM
Perl: suppress std output and std error say_hi_ravi Programming 1 11-01-2012 06:22 AM
How to catch system-level exceptions in Linux cyker Programming 2 01-25-2011 06:04 AM
Template algorithm for std::vector and std::string CartmanYO Programming 4 09-08-2010 09:56 AM


All times are GMT -5. The time now is 10:38 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration