Function Interposition for C++ Operator new

A

Abhi

Recently only I was introduced to this very nice concept of Function
Interposition in C. I tried googling it a bit further for C++, but
could not find much info.
Could someone please help me in applying the interposition concept to
replace the calls for new and delete with my own calls.

Thanks.
 
A

Anand Hariharan

Recently only I was introduced to this very nice concept of Function
Interposition in C. I tried googling it a bit further for C++, but
could not find much info.
Could someone please help me in applying the interposition concept to
replace the calls for new and delete with my own calls.

Thanks.

Never heard of "Function Interposition" myself, but "placement new" is
probably what you are looking for.

- Anand
 
I

Ian Collins

Recently only I was introduced to this very nice concept of Function
Interposition in C. I tried googling it a bit further for C++, but
could not find much info.
Could someone please help me in applying the interposition concept to
replace the calls for new and delete with my own calls.

You don't have to resort to interposing for the new and delete
operators, just provide your own and the language will do the rest!
 
A

Abhi

You don't have to resort to interposing for the new and delete
operators, just provide your own and the language will do the rest!


Yes u r correct, overloading new and delete is one option, but that
would
call for recompiling. Interposition however will let me do the same
without recompiling
Reading this, http://www.jayconrod.com/cgi/view_post.py?23
will provide better understanding of the problem

Thanks
Abhishek
 
I

Ian Collins

Yes u r correct, overloading new and delete is one option, but that
would
call for recompiling. Interposition however will let me do the same
without recompiling

You still have to compile something!
Reading this, http://www.jayconrod.com/cgi/view_post.py?23
will provide better understanding of the problem

As a long time Unix programmer, I am very familiar with function
interposition.

The same rules apply to C++, but there in an extra layer of complexity
in the form of name mangling. In order to interpose a C++ function, you
have to know it's mangled name. That name will be compiler specific.
 
M

Marc

Ian said:
The same rules apply to C++, but there in an extra layer of complexity
in the form of name mangling. In order to interpose a C++ function, you
have to know it's mangled name.

Do you? The object that you use to interpose will be compiled by a C++
compiler which will be responsible for mangling, as usual. You do have
to be careful to give the function the exact same signature though.
 
8

88888 Dihedral

I need to use an instance that has a list of methods to be hooked by my users without accessing my source code and deriving new classes. Can someone give some useful advice about the properties in the class I am desired without using the a function pointer in the old C language?
 
8

88888 Dihedral

Bill Gates do know C well in Win 95-98. But I am thinking is the system written in C or C++.
 
F

Fred Zwarts \(KVI\)

"88888 Dihedral" wrote in message
Bill Gates do know C well in Win 95-98. But I am thinking is the system
written in C or C++.

Is that a question or an observation? Cogito ergo sum?
What is the relation with the subject "Function Interposition for C++
Operator new"?
 
A

Abhi

Ian Collins  wrote:

Do you? The object that you use to interpose will be compiled by a C++
compiler which will be responsible for mangling, as usual. You do have
to be careful to give the function the exact same signature though.

Thanks Ian and Mark for your help.
I successfully interposed my implementation of new :)
As suggested correctly, the same rules apply to C++ as well. So what
all i did was -
1. Created a new .cc file and defined my operator new. Compiled this
into a shared object (g++ -ldl -fPIC -shared FakeNew.cc -o
libfakenew.so)
2. set LD_PRELOAD to point to this shared library
3. Executed my old program (no recompiling)
4. All the calls to new now calls my implementation

I was not blocked by name mangling as such, and even if it does play
some role, then we have utilities like nm and c++filt to help us.

Abhishek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top