Proper Way to catch exception thrown by new keyword

F

Fao, Sean

I'm reading The C++ Programming Language by Bjarne Stroustrup and I
was unclear as to the proper way to catch an exception thrown by the
new keyword. I was wondering if someobody here could let me know if I
have written the following code correctly.

<CODE_SNIP>
#include <iostream>
#include <new>

using std::cerr;
using std::bad_alloc;

int main()
{
char *p;

try
{
for(; ;)
p = new char[100000];
}

catch(bad_alloc)
{
cerr << "Error allocating memory...\n";
return 1;
}

delete p;

return 0;
}

</CODE_SNIP>

I thank you in advance for any pointers you can provide.

Sean
 
J

John Harrison

Fao said:
I'm reading The C++ Programming Language by Bjarne Stroustrup and I
was unclear as to the proper way to catch an exception thrown by the
new keyword. I was wondering if someobody here could let me know if I
have written the following code correctly.

Well that depends on what you want it to do.
<CODE_SNIP>
#include <iostream>
#include <new>

using std::cerr;
using std::bad_alloc;

int main()
{
char *p;

try
{
for(; ;)
p = new char[100000];

Are you trying to loop until you run out of memory?

If two allocations succeed then this loop will leak memory. Because the
second allocation will overwrite the first and you will never be able to
free that memory.
}

catch(bad_alloc)

This isn't wrong but usually you catch exceptions using a const reference,
to avoid copying the exception object unnecessarily.

catch (const bad_alloc&)
{
cerr << "Error allocating memory...\n";
return 1;
}

delete p;

return 0;
}

</CODE_SNIP>

I thank you in advance for any pointers you can provide.

Sean

Looks OK.

john
 
U

Unforgiven

delete p;

In addition to what join said, this line will always be reached, even when
an exception occurs. You may want to add a check (initialise p to NULL and
check for that for instance, or place the delete still inside the try block)
whether you really have any memory to delete.

Also you are allocating with new[], do you should free with delete[]. It
doesn't make any difference here because char is a basic type, but it's good
practice to do it anyway: it adds clarity and gets you in the habit so you
don't forget it when it does make a difference.

--
Unforgiven

"Earth. It exists only in a corner of my memory."
Lord Dornkirk
The Vision of Escaflowne
 
J

John Harrison

Unforgiven said:
In addition to what join said, this line will always be reached, even when
an exception occurs. You may want to add a check (initialise p to NULL and
check for that for instance, or place the delete still inside the try block)
whether you really have any memory to delete.

Actually there's a return in the catch block, so the delete is not reached
when an exception occurs.
Also you are allocating with new[], do you should free with delete[]. It
doesn't make any difference here because char is a basic type, but it's good
practice to do it anyway: it adds clarity and gets you in the habit so you
don't forget it when it does make a difference.

Good point.

john
 
U

Unforgiven

John said:
Actually there's a return in the catch block, so the delete is not
reached when an exception occurs.

Oops, didn't see that ^_^

--
Unforgiven

"Earth. It exists only in a corner of my memory."
Lord Dornkirk
The Vision of Escaflowne
 
F

Fao, Sean

Fao said:
I'm reading The C++ Programming Language by Bjarne Stroustrup and I
was unclear as to the proper way to catch an exception thrown by the
new keyword. I was wondering if someobody here could let me know if I
have written the following code correctly.

I appreciate your comments, I have rewritten the code using your
recomendations.

Thank you much...

Sean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top