qt, -fno-rtti i okolice

Marcin 'Qrczak' Kowalczyk qrczak w knm.org.pl
Śro, 8 Gru 1999, 20:38:43 CET


Wed, 08 Dec 1999 14:02:52 +0100, Marcin Dalecki <dalecki w dacotec.net> pisze:

> Tak jak mówiłem jeśli operator new rzuca wyjątkiem, to tak czy siak
> wywołanie new zwraca jakąś wartość, tylko, że niekoniecznie właściwą.

Wyrażenie new wtedy rzuca ten sam wyjątek.

http://www.cerfnet.com/~mpcline/C++-FAQs-Lite/

==============================================================================

[16.5] Do I need to check for NULL after p = new Fred()?

No! (But if you have an old compiler, you may have to force the compiler to
have this behavior[16.6]).

It turns out to be a real pain to always write explicit NULL tests after every
new allocation.  Code like the following is very tedious:

    Fred* p = new Fred();
    if (p == NULL)
      throw bad_alloc();

If your compiler doesn't support (or if you refuse to use) exceptions[17], your
code might be even more tedious:

    Fred* p = new Fred();
    if (p == NULL) {
      cerr << "Couldn't allocate memory for a Fred" << endl;
      abort();
    }

Take heart.  In C++, if the runtime system cannot allocate sizeof(Fred) bytes
of memory during p = new Fred(), a bad_alloc exception will be thrown.  Unlike
malloc(), new never returns NULL!

Therefore you should simply write:

    Fred* p = new Fred();   // No need to check if p is NULL

However, if your compiler is old, it may not yet support this.  Find out by
checking your compiler's documentation under "new".  If you have an old
compiler, you may have to force the compiler to have this behavior[16.6].

==============================================================================

-- 
 __("<    Marcin Kowalczyk * qrczak w knm.org.pl http://qrczak.ids.net.pl/
 \__/          GCS/M d- s+:-- a22 C+++>+++$ UL++>++++$ P+++ L++>++++$ E-
  ^^                W++ N+++ o? K? w(---) O? M- V? PS-- PE++ Y? PGP->+ t
QRCZAK                  5? X- R tv-- b+>++ DI D- G+ e>++++ h! r--%>++ y-



Więcej informacji o liście dyskusyjnej pld-devel-pl