Null Pointer

Discussion in 'C++' started by Michael, Aug 1, 2006.

  1. Michael

    Michael Guest

    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
     
    Michael, Aug 1, 2006
    #1
    1. Advertising

  2. Michael

    Phlip Guest

    Michael wrote:

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

    --
    Phlip
    http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
     
    Phlip, Aug 1, 2006
    #2
    1. Advertising

  3. Michael

    Daniel T. Guest

    In article <>,
    "Michael" <> wrote:

    > 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.
     
    Daniel T., Aug 1, 2006
    #3
  4. Michael

    mlimber Guest

    Michael wrote:
    > 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
     
    mlimber, Aug 1, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    27,520
    Mike Schilling
    Mar 29, 2006
  2. Ken
    Replies:
    69
    Views:
    12,673
    Michael D. Borghardt
    Jul 31, 2004
  3. Ken
    Replies:
    5
    Views:
    2,588
    Victor Bazarov
    Jul 24, 2004
  4. aneuryzma
    Replies:
    3
    Views:
    781
    Jim Langston
    Jun 16, 2008
  5. Christopher
    Replies:
    4
    Views:
    465
    Ruben Safir
    Jul 9, 2011
Loading...

Share This Page