Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Use of nothrow in new() with aC++ compiler on HP-UX11 system

 
Shashibhushan Gokhale
Occasional Contributor

Use of nothrow in new() with aC++ compiler on HP-UX11 system

Dear friends,

I am using HP aC++ compiler(aCC) on HP-UX11 OS to compile C/C++ programs.

I want to know the usage of nothrow in the operator new() on C++ so as to avoid the new to throw an bad_alloc exception if new is unable to allocate the memory.

It would be better if the explanation would be assisted with a small example illustrating the concept.

Expecting Reply,
Thanks in Advance,

Yours,
Shashibhushan Gokhale
3 REPLIES 3
Adam J Markiewicz
Trusted Contributor

Re: Use of nothrow in new() with aC++ compiler on HP-UX11 system

First the concept.
Good old malloc() used NULL for failure. But the programmers found it borring to check it every time the malloc() was called. So they invented mechanism to stop execution just in the place the error occured and decide what to do next. That's how the exceptions were born. But the exceptions are 'heavy'. It is extremally more work with handling exception than in returning NULL. It is not seen from the programmer's point of view, as it is duty for the compiler to add it.

That's why the programmers wanted to have a choice.

The first sollution was set_new_handler(new_handler new_p) function. Bad idea. Global function with global handling. And if there were, for example two libraries that wanted to install the handler, there would be one winner and one looser.

So now you have special version of operator new for not throwing the exceptions everytime you don't want them. You can mix calling the operators in the same code.

The usage is:

class C {
};

C *ptr1 = new C; // this throws exception by default, unless set_new_handler(new_handler new_p) installs something different
C *ptr2 = new(nothrow) C; // this never throws any exception, but remember to check for NULL!

And the last thing is: If the operator new returns, but the value is NULL, the compiler doesn't call the constructor for the class.

If you still have any doubt, feel free to ask.

Adam
I do everything perfectly, except from my mistakes
Kevin Huang_3
New Member

Re: Use of nothrow in new() with aC++ compiler on HP-UX11 system

Hi,

Dose the following neams that "nothrow" won't work, therefore a "bad_alloc" exception is thrown if "new" is unable to allocate the memory?

Thanks in advanced!

-Kevin

//test.cpp
#include
#include
using namespace std;

class myClass { };

int main()
{
myClass * x = new(nothrow) myClass;

return 0;
}
//test.cpp ends

$ aCC -AA test.cpp
Warning 930: "test.cpp", line 9 # Placement operator delete invocation is not yet implemented.
myClass * x = new(nothrow) myClass;
^^^^^^^^^^^^^^^^^^^^

Where,
$ uname -a
HP-UX lciahp8 B.11.23 U ia64 0582466038 unlimited-user license
$ aCC -V
aCC: HP aC++/ANSI C B3910B A.05.50 [May 15 2003]
Adam J Markiewicz
Trusted Contributor

Re: Use of nothrow in new() with aC++ compiler on HP-UX11 system

No.

I've just checked it on HP sites. This warning is quite new.
It means that if the _constructor_ of the class throws an exeption, the required cleanup is not added correctly by the compiler.

However the operator new itself should work as expected.

See:
http://h21007.www2.hp.com/dspp/ml/showArchiveMessage/1,,24!02!11!0118,00.html


Good luck
Adam
I do everything perfectly, except from my mistakes