Null Pointer

M

Michael

Hi,

Could you tell me how to under the following statements? Does it mean
each and every memory allocation will check all the pointer no matter a
null pointer or not? Right? Thanks in advance!

This nothrow method requires more work than the exception method, since
the value returned has to be checked after each and every memory
allocation.

Michael
 
P

Phlip

Michael said:
Could you tell me how to under the following statements? Does it mean
each and every memory allocation will check all the pointer no matter a
null pointer or not? Right? Thanks in advance!

This nothrow method requires more work than the exception method, since
the value returned has to be checked after each and every memory
allocation.

This article covers that nicely:

http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=170&rl=1

I got it with this:

http://www.google.com/search?q=new+nothrow

Always prep a question with Google before posting it.
 
D

Daniel T.

"Michael said:
Hi,

Could you tell me how to under the following statements? Does it mean
each and every memory allocation will check all the pointer no matter a
null pointer or not? Right? Thanks in advance!

This nothrow method requires more work than the exception method, since
the value returned has to be checked after each and every memory
allocation.

It means that every time you use the nothrow version of new, *you* have
to manually check the value returned to make sure it isn't null.
 
M

mlimber

Michael said:
Could you tell me how to under the following statements? Does it mean
each and every memory allocation will check all the pointer no matter a
null pointer or not? Right? Thanks in advance!

This nothrow method requires more work than the exception method, since
the value returned has to be checked after each and every memory
allocation.

It means that there are two methods: a nothrow method, which doesn't
emit any exceptions, and a throwing method, which may emit an
exception. Presumably, both methods return a pointer to something, but
the nothrow method might return a null pointer to indicate failure,
while the throwing method will always return a valid pointer since it
will throw an exception on failure instead. Here's an example:

#include <iostream>
#include <new>
#include <cstdlib>


struct A
{
static A* Create( const std::nothrow_t& ) throw()
{
A* a = new( std::nothrow ) A;
return a; // might be null
}

static A* Create()
{
A* a = new A;
return a; // guaranteed to be non-null; see FAQ 16.6
}
// ...
};

void Foo()
{
A* const a1 = A::Create( std::nothrow );
if( !a1 )
{
std::cerr << "Doh! Couldn't create a1." << std::endl;
std::exit( -1 ); // or whatever
}

A* const a2 = A::Create( std::nothrow );
if( !a2 )
{
std::cerr << "Doh! Couldn't create a2." << std::endl;
std::exit( -1 ); // or whatever
}

// Finally, we can use a1 and a2
}

void Bar()
{
try
{
A* const a1 = A::Create();
A* const a2 = A::Create();
// Use a1 and a2 here.
}
catch( const std::exception& e )
{
std::cerr << "Doh! " << e.what() << std::endl;
std::exit( -1 ); // or whatever
}
}

See FAQ 17.1 on the why of using exceptions. The gist: When we have
multiple statements that could fail, the exception approach is often
cleaner because it avoids a bunch of if-statements that clutter the
flow and logic of the code and means the user can't ignore the error
(intentionally or not).

Cheers! --M
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top