You can define your own versions of operator new() and operator delete() in C++ applications. In applications that use shared libraries, it may be useful for the shared library to use a user defined operator new() and operator delete() in the main application executable. You may want to do this if you want more control over memory management than if you use the default calls to these operators in the C++ Runtime Library libC.a. Enabling this facility in your applications requires using the runtime linking option -brtl, creating an export list with the mangled names for the operators you are defining, and building your applications with the correct link time option so that calls to operator new() and operator delete() are replaceable. The mangled names indicated by the export list are then available to the runtime environment so that libraries loaded at run time use your versions of operator new() and operator delete().
Follow these steps:
#include <new>
#include <cstdio>
#include <cstdlib>
void* operator new(unsigned long x) {
printf("operator new %ld\n", x);
return malloc(x);
}
int main() {
return 5;
}
xlC -c my_app.CCreates my_app.o.
nm -epC my_app.oThe nm command displays a listing similar to this:
.__nw__FUl T 0 TOC d 56 __nw__FUl D 60 12 __nw__FUl d 56 4__nw__FUl is a valid symbol listed in Table 1. Add this symbol to your export list.
xlC my_app.o -bE:my_app.exp -brtlWhere my_app.exp is the export file that you created in step 2.
| Mangled names | |
|---|---|
| Operator new and vector new names when compiling with -qlanglvl=nonewexcp |
|
| Operator new and vector new names when compiling with -qlanglvl=newexcp |
|
| Operator delete names |
|