473,405 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Is it good to assert after new() everytime

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 }
.....

Mar 26 '07 #1
9 2235
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 26 '07 #2
On 26 Mar, 12:52, "Alf P. Steinbach" <a...@start.nowrote:
* 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.
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.

--
Erik Wikström

Mar 26 '07 #3
Achintya wrote:
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.
Mar 26 '07 #4
"Erik Wikström" <er****@student.chalmers.sewrote in message
news:11**********************@y66g2000hsf.googlegr oups.com...
On 26 Mar, 12:52, "Alf P. Steinbach" <a...@start.nowrote:
* Achintya:
<snip>
>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

Mar 27 '07 #5
jk
On 26 Mar 2007 03:36:59 -0700, "Achintya"
<vs*************@agilent.comwrote:
>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.

Mar 27 '07 #6
Erik Wikström schrieb:
On 26 Mar, 12:52, "Alf P. Steinbach" <a...@start.nowrote:
>* 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.

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
>
--
Erik Wikström
Mar 29 '07 #7
On Mar 29, 11:37 pm, "J.M." <jm_jm_remove_t...@gmx.dewrote:
Erik Wikström schrieb:
On 26 Mar, 12:52, "Alf P. Steinbach" <a...@start.nowrote:
* 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.
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.
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.
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...
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Mar 30 '07 #8

"James Kanze" <ja*********@gmail.comwrote in message
news:11*********************@d57g2000hsg.googlegro ups.com...
On Mar 29, 11:37 pm, "J.M." <jm_jm_remove_t...@gmx.dewrote:
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
Mar 31 '07 #9
On Mar 31, 10:49 pm, "JohnQ" <johnqREMOVETHISprogram...@yahoo.com>
wrote:
"James Kanze" <james.ka...@gmail.comwrote in message
news:11*********************@d57g2000hsg.googlegro ups.com...
On Mar 29, 11:37 pm, "J.M." <jm_jm_remove_t...@gmx.dewrote:
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?
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.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 1 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: SOAP | last post by:
how to compile java with assert statement I failed to compile this simple example public class Foo { public void m1( int value ) { assert 0 <= value; System.out.println( "OK" ); }
28
by: Fábio Mendes | last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with things or the python-list sent my msg to /dev/null. I couldn't find anything related in previous PEP's, so here it goes a...
12
by: Siemel Naran | last post by:
What is a good idiom for handling a lazy object? I see 2 good possibilities. Any more, any comments? Which way do people here use? (1) class Thing { public: Thing(double x, double y) :...
7
by: Stephen Tyndall | last post by:
I know the preprocessor is evil, but I'd like to know what's going on in the following code. The problem is when the num variable is used in the ASSERT macro inside main(). When running the...
47
by: Rob Thorpe | last post by:
In general, is it considered bad practice to use asserts in production code? What about writing a macro that does the same as assert but continues to work regardless of the state of NDEBUG? I...
19
by: Fernando Cacciola | last post by:
I'm puzzled, Why and how _exactly_ is this: void Foo<T>(T v ) where T : Interface/Value/Class/class any better than this void Foo( Interface/Value/Class/object v )
13
by: priyanka | last post by:
Hi there, Can anyone show me how the assert() function works ? I need to develop my own assert() function instead of using the one defined in the assert.h file. It would be great if anyone could...
24
by: asincero | last post by:
Would it be considered good form to begin every method or function with a bunch of asserts checking to see if the parameters are of the correct type (in addition to seeing if they meet other kinds...
1
by: murali026 | last post by:
Hi All, I have a code shown below. Could you please help me in assisting with the functionality of the assert. The code is very huge and here i am explaining here with a small portion of it....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.