0
class A
{
public:
    A()
    {
        B* b = new B();
        if (b == NULL) 
        {
            ERROR_CODE = ? // what will be an appropriate error code?
        }
    }    
};

class B
{
public:
    uint32 *p;

    B()
    {
        p = new uint32[16];
        memset(p, 0x00, 16*sizeof(uint32));
        memset(p, 0xFF, 8*sizeof(uint32));
    }
};

What will be the appropriate OS error code - invalid_handle, out_of_memory, invalid_operation, or anything else?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
suprit
  • 11
  • 1
  • You could try catching `std::bad_alloc` exceptions with `new` – πάντα ῥεῖ Nov 18 '22 at 12:35
  • 1
    you would need nothrow to avoid exception – stark Nov 18 '22 at 12:36
  • 1
    Handling out of memory conditions with an error code is pretty much impossible. throwing an exception is better approach. You don't seem to be aware that `new` throws an exception when allocation fails, it does not (by default) return NULL. – john Nov 18 '22 at 12:58

0 Answers0