[C++] Cum semnaleaza eroare operatorul standard 'new'?

Despre limbajul de programare C++ si biblioteca standard STL (forum moderat)
Post Reply
User avatar
Ovidiu Cucu
Fondator
Fondator
Posts: 3776
Joined: 11 Jul 2007, 16:10
Judet: Iaşi
Location: Iasi
Contact:

[C++] Cum semnaleaza eroare operatorul standard 'new'?

Post by Ovidiu Cucu » 22 Mar 2009, 13:14

Intrebare Cum semnaleaza eroare operatorul standard 'new'?

Raspuns
In headerul standard <new> sunt declarate urmatoarele functii pentru alocare dinamica:
  1. Code: Select all

    void* operator new(std::size_t size) throw(std::bad_alloc);
  2. Code: Select all

    void* operator new(std::size_t size, const std::nothrow_t&) throw();
  3. Code: Select all

    void* operator new[](std::size_t size) throw(std::bad_alloc);
  4. Code: Select all

    void* operator new[](std::size_t size, const std::nothrow_t&) throw();
In caz de eroare (operatorul new nu reuseste sa aloce memoria ceruta)...
  • ...(1) si (3), arunca exceptii de tip bad_alloc.
    Exemplu

    Code: Select all

    #include <iostream>
    int main()
    {
       try
       {
          // try to allocate a large amount of memory; if fails, 'new' throws an exception.
          char* buffer = new char[0x7ffffffe];
          std::cout << "Allocation succeeded." << std::endl;
          delete []buffer;
       }
       catch(std::bad_alloc& e)
       {
          // handle standard bad_alloc exception.
          std::cout << "Exception: " << e.what() << std::endl;
       }
    }
  • ...(2) si (4) nu arunca exceptii, in schimb intorc un pointer NULL.
    Exemplu

    Code: Select all

    #include <iostream>
    int main()
    {
       // try to allocate a large amount of memory; if fails, 'new' returns a NULL pointer.
       char* buffer = new (std::nothrow) char[0x7ffffffe];
       if(NULL == buffer)
       {
          std::cout << "Allocation failed." << std::endl;
       }
       else
       {
          std::cout << "Allocation succeeded." << std::endl;
          delete []buffer;
       }
    }

Next FAQ >>



Viorel
Microsoft MVP
Microsoft MVP
Posts: 285
Joined: 13 Jul 2007, 12:26

Re: [C++] Cum semnaleaza eroare operatorul standard 'new'?

Post by Viorel » 22 Mar 2009, 14:24

NOTE:
  • Cu ajutorul funcţiei _set_new_handler poate fi instalată o procedură care să elibereze memoria şi să reîncerce alocarea eşuată.

Next FAQ >>

User avatar
Ovidiu Cucu
Fondator
Fondator
Posts: 3776
Joined: 11 Jul 2007, 16:10
Judet: Iaşi
Location: Iasi
Contact:

Re: [C++] Cum semnaleaza eroare operatorul standard 'new'?

Post by Ovidiu Cucu » 08 May 2009, 14:14

O alternativa la apropunerea lui Viorel este sa se combine cele doua versiuni de 'new'.

Code: Select all

   try
   {
      // if first allocation attempt fails 'new' returns a NULL pointer
      char* buffer = new (std::nothrow) char[size];
      if(NULL == buffer)
      {
         // do some cleaning if possible and/or compact the heap, then try again.
         // ...
         ::HeapCompact(::GetProcessHeap(), 0);
         // in case of failure (this time, it's really bad) an exception is thrown.
         buffer = new char[size];
      }
      // ...
   }

Next FAQ >>

Post Reply