Is it good to assert after new() everytime

A

Achintya

Is it good to assert the pointer *each* time after a new() is called?
or it should be a normal if condition check. which of below is good
practice: (I know assert works only in debug)
1)
......
int* i;
i = new int;
assert( i )
.....
OR
2)
.....
int* i;
i = new int;
if( i )
{
//do something
}
else { //do something }
.....
 
A

Alf P. Steinbach

* Achintya:
Is it good to assert the pointer *each* time after a new() is called?
or it should be a normal if condition check. which of below is good
practice: (I know assert works only in debug)
1)
.....
int* i;
i = new int;
assert( i )
....
OR
2)
....
int* i;
i = new int;
if( i )
{
//do something
}
else { //do something }
....

Both are ungood.

In standard C++ you will never get a nullpointer from ordinary 'new'.

If 'new' fails you get a std::bad_alloc exception.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

* Achintya:




Both are ungood.

In standard C++ you will never get a nullpointer from ordinary 'new'.

If 'new' fails you get a std::bad_alloc exception.

I never personally check for those exceptions (though they might be
caught in some generic catch-all in main) because in most cases the
risk of running out of memory is very low. And should you ever run
into it it's usually not much you can do about it anyway except
terminating your app.
 
J

Jacek Dziedzic

Achintya said:
Is it good to assert the pointer *each* time after a new() is called?
or it should be a normal if condition check. which of below is good
practice: (I know assert works only in debug)
1)
.....
int* i;
i = new int;
assert( i )
....
OR
2)
....
int* i;
i = new int;
if( i )
{
//do something
}
else { //do something }
....

In fact, neither. New (used like this) never returns a null
pointer. You should:

try {
int* i = new int;
// do something
}
catch(std::bad_alloc) {
// out of memory
}

HTH,
- J.
 
D

Dennis \(Icarus\)

* Achintya:
I never personally check for those exceptions (though they might be
caught in some generic catch-all in main) because in most cases the
risk of running out of memory is very low. And should you ever run
into it it's usually not much you can do about it anyway except
terminating your app.

Nah, there are ways to deal with it much more gracefully.

Dennis
 
J

jk

Is it good to assert the pointer *each* time after a new() is called?
or it should be a normal if condition check. which of below is good
practice: (I know assert works only in debug)
1)
.....
int* i;
i = new int;
assert( i )
....
OR
2)
....
int* i;
i = new int;
if( i )
{
//do something
}
else { //do something }
....

i don't quite understand what you are trying to achieve?

if we for sake of argument pretend that new would return a null ptr,
what could you possible gain of an assert here?

assert is better for catching programming errors and not runtime
conditions e.g. checking function in-parameters.
 
J

J.M.

Erik said:
I never personally check for those exceptions (though they might be
caught in some generic catch-all in main) because in most cases the
risk of running out of memory is very low.

That depends a lot on your application...
And should you ever run
into it it's usually not much you can do about it anyway except
terminating your app.

Well, you could always stop that particular operation that the user wanted
and give him the option of doing something else.. Letting a program crash
is not really an acceptable to me...

Jan
 
J

James Kanze

Erik Wikström schrieb:

If you're not catching the exception, you should set the new
handler so that it doesn't occur. (Most of my programs set the
new handler to generate an error message and abort.)
That depends a lot on your application...

And the context in which it runs... On many systems (Linux,
Windows), you can't handle out of memory anyway, at least in the
default configurations. And on most systems, you can't
systematically handle it---only if it occurs in a new.
Well, you could always stop that particular operation that the user wanted
and give him the option of doing something else.. Letting a program crash
is not really an acceptable to me...

Which is why you set the new handler.

Interrupting a given operation but continuing to handle others
is a good solution for many applications, if:

-- operations are more or less open, so that you cannot know
in advance the amount of memory an operation might need,

-- you can be sure that the out of memory condition will occur
during a new, and not, say, because of stack overflow (most
of the cases I've seen of "open" operations have involved
recursive descent parsing, which means that stack overflow
is more likely than out of memory), and

-- you are sure that the systems you are running on are
configured so that you can actually detect the condtion:
this is not the default configuration for Linux, and the one
time I experimented under Windows NT, I couldn't get an out
of memory condition either.

As a general rule, unless you take a number of special
precautions, you cannot exclude your programming crashing
because of a lack of memory.
 
J

JohnQ

the one
time I experimented under Windows NT, I couldn't get an out
of memory condition either.

Do you mean working with a compiler-supplied memory manager you couldn't or
that you built a memory manager directly on top of the virtual memory system
and couldn't identify out-of-mem?

John
 
J

James Kanze

"James Kanze" <[email protected]> wrote in message
Do you mean working with a compiler-supplied memory manager you couldn't or
that you built a memory manager directly on top of the virtual memory system
and couldn't identify out-of-mem?

Well, I only tried with the compiler-supplied memory manager
(malloc, in my test case). I *think* that the problem was at
the system level, however, and I also wouldn't be at all
surprised to find that it is configurable, and that the code
works with other configurations. However, every time I tried to
exhaust memory, the system would first automatically increase
the swap space, and then pop-up a window telling me that there
wasn't enough memory, and asking me to kill some applications to
make more memory available. In no case did I ever return from
malloc without having successfully allocated memory.

But as I say, that was just one particular test, and I have no
idea whether it depends on some configuration parameters or not.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top