[Bro] pcap_next Question

Chris Crawford christopher.p.crawford at gmail.com
Thu Aug 9 03:43:47 PDT 2012


I don't think that is quite right for C++.

In this case, I have a stack trace showing that std::bad_alloc is
thrown, but never caught.

For a less official explanation, here's a stackoverflow post that
addresses the same issue:
http://stackoverflow.com/questions/7277637/new-stdnothrow-vs-new-within-a-try-catch-block

Cplusplus.com has a more official explanation.

Here is a high level view of how new works in the C++ STL:
http://www.cplusplus.com/reference/std/new/

If you take a look at both new and new[], you can see that in either
case "On failure, it throws a bad_alloc exception."

This explains std::nothrow and how to catch the error:
http://www.cplusplus.com/reference/std/new/bad_alloc/

To get the effect you're talking about in C++, you have to explicitly
cast a new operation with std::nothrow:
http://www.cplusplus.com/reference/std/new/nothrow/

Here is another example:
http://www.cprogramming.com/tips/tip/cincrement-new-does-not-return-0-on-failure

I did a bit a of Googling to find some documentation that would say
that the new operator returns a NULL pointer by defaut, versus
returning std::bad_alloc.  The only thing I could really find was a an
bug in Visual C++ that was documented in this KB article:
http://support.microsoft.com/?kbid=167733


-Chris

On Thu, Aug 9, 2012 at 2:01 AM, Vern Paxson <vern at icir.org> wrote:
>>        block = new u_char[size];
>>        if ( ! block )
>>                 reporter->InternalError("out of memory");
>>
>>
>> Since the new operator is throwing a std::bad_alloc error, that line
>> should be surrounded with a bit of error handling like in the attached
>> patch file.
>
> I thought 'new' is defined to return a nil pointer upon memory exhaustion.
> Is that wrong / dates me horribly?
>
>                 Vern



More information about the Bro mailing list