As I’m coding for both Windows and Linux, I have a whole host of problems.
Microsoft Visual C++ has no stdint header, but for that I wrote my own.
Now I found out that MS C++’s new operator does not throw an exception, so I want to fix this a quickly as possible. I know I can define a Macro with Parameters in Parenthesis, can I define a macro that replaces
MyClass x = new MyClass();
with
#ifdef MSC_VER
if(!(MyClass x = new MyClass())
{
throw new std::bad_alloc();
}
#else
MyClass x = new MyClass();
#endif
(or something equivalent), AND works in BOTH MS C++ and G++ ?
Or alternatively if that is not possible, a batch file to run over the Code to do this?
I have become rather dependent on this exception being thrown.
The simplest alternative is to write your own implementation of
operator new, overriding the compiler’s default implementation (if your version of MSVC supports it). This is what Roger also suggested in his linked answer.The following program illustrates the attempt.
operator newallocates memory from the heap usingmallocandfree. If you’re doing conditional compilation for G++ and MSVC, you can also useHeapAllocandHeapFreeinstead ofmallocandfreefor a more WinAPI-oriented approach.