Aracılığıyla paylaş


new operator (STL Samples)

Nasıl kullanılacağı gösterilmiştir Yeni işleç in <new>.

void *operator new(
   size_t n
)
void *operator new(
   size_t n,
   const nothrow&
)
void *operator new[](
   size_t n
);

Notlar

[!NOT]

Prototip sınıfı/parametre adları üstbilgi dosyasında sürüm eşleşmiyor.Bazıları, okumayı kolaylaştırmak için değiştirildi.

İlk Yeni işleci bellek dener ve başarısız olursa, bir özel durum oluşturur.Yeni ikinci işleci türü nothrow ikinci bir parametre kabul eder.Bu parametre ayırma başarısız olursa, okuyor null dönmek ve bir özel durum yok olduğunu gösterir.Yeni üçüncü operatör için bir dizi türünde bellek ayırır ve başarısız olursa, bir özel durum oluşturur.

Örnek

// newop.cpp
// compile with: /EHsc
//
// Functions:
//   void *operator new(size_t n)
//   void *operator new(size_t n, const nothrow&)
//   void *operator new[](size_t n);

#include <new>
#include <iostream>
using namespace std;

class BigClass {
public:
   BigClass() {};
   ~BigClass(){}

#ifdef _WIN64
      double BigArray[0x0fffffff];
#else
      double BigArray[0x0fffffff];
#endif
};

int main() {
   try {
      BigClass * p = new BigClass;
   }

   catch( bad_alloc a) {
      const char * temp = a.what();
      cout << temp << endl;
      cout << "Threw a bad_alloc exception" << endl;
   }

   BigClass * q = new(nothrow) BigClass;

   if ( q == NULL )
      cout << "Returned a NULL pointer" << endl;

   try {
      BigClass * r[3] = {new BigClass, new BigClass, new BigClass};
   }

   catch( bad_alloc a) {
      const char * temp = a.what();
      cout << temp << endl;
      cout << "Threw a bad_alloc exception" << endl;
   }
}

Örnek Çıktı

bad allocation
Threw a bad_alloc exception
Returned a NULL pointer
bad allocation
Threw a bad_alloc exception

Gereksinimler

Başlık: <new>

Ayrıca bkz.

Kavramlar

Standart şablon kitaplığı örnekleri