Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Malloc in C++ constructor

I have to interface with some C code from C++ class constructor (Intel library)


  class A{
    A{
     x = ippiMalloc();
     if(x==NULL) ...
    }
  }

In the constructor malloc function (intel version) is used. If ippiMalloc function do not succeed what is the correct way to handle it. Throw exception?

like image 676
Ross Avatar asked Oct 10 '10 11:10

Ross


1 Answers

Yes, an exception would likely be the most appropriate way to handle the error here -- at least it would be the cleanest way. This is also what the "new" operator would do on failure.

If your app isn't exception aware than you have a problem. In which case a factory method on the class might make sense.

static A * create() { ... }

Here you can wrap this into a non-exception program, likely returning null if you can't create the object.

like image 118
edA-qa mort-ora-y Avatar answered Oct 10 '22 04:10

edA-qa mort-ora-y