Function Interposition for C++ Operator new

Discussion in 'C++' started by Abhi, Oct 27, 2011.

  1. Abhi

    Abhi Guest

    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.
     
    Abhi, Oct 27, 2011
    #1
    1. Advertising

  2. On Oct 27, 3:40 pm, Abhi <> wrote:
    > 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
     
    Anand Hariharan, Oct 27, 2011
    #2
    1. Advertising

  3. Abhi

    Ian Collins Guest

    On 10/28/11 09:40 AM, Abhi wrote:
    > 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!

    --
    Ian Collins
     
    Ian Collins, Oct 28, 2011
    #3
  4. Abhi

    Abhi Guest

    On Oct 28, 5:31 am, Ian Collins <> wrote:
    > On10/28/11 09:40 AM, Abhi wrote:
    >
    > > 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!
    >
    > --
    > 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
    Reading this, http://www.jayconrod.com/cgi/view_post.py?23
    will provide better understanding of the problem

    Thanks
    Abhishek
     
    Abhi, Oct 28, 2011
    #4
  5. Abhi

    Ian Collins Guest

    On 10/28/11 06:31 PM, Abhi wrote:
    > On Oct 28, 5:31 am, Ian Collins<> wrote:
    >> On10/28/11 09:40 AM, Abhi wrote:
    >>
    >>> 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!

    >
    > 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.

    --
    Ian Collins
     
    Ian Collins, Oct 28, 2011
    #5
  6. Abhi

    Marc Guest

    Ian Collins wrote:

    > 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.
     
    Marc, Oct 28, 2011
    #6
  7. 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?
     
    88888 Dihedral, Oct 28, 2011
    #7
  8. Bill Gates do know C well in Win 95-98. But I am thinking is the system written in C or C++.
     
    88888 Dihedral, Oct 28, 2011
    #8
  9. "88888 Dihedral" wrote in message
    news:32543630.2684.1319809875005.JavaMail.geo-discussion-forums@prog16...
    >
    >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"?
     
    Fred Zwarts \(KVI\), Oct 28, 2011
    #9
  10. Abhi

    Abhi Guest

    On Oct 28, 12:33 pm, Marc <> wrote:
    > Ian Collins  wrote:
    > > 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.


    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
     
    Abhi, Oct 28, 2011
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David C. Partridge
    Replies:
    1
    Views:
    3,335
    Chris Smith
    Apr 13, 2004
  2. Replies:
    8
    Views:
    355
    Neil Cerutti
    Dec 22, 2005
  3. Replies:
    1
    Views:
    330
    Ian Collins
    Jun 21, 2008
  4. xmllmx
    Replies:
    6
    Views:
    437
    xmllmx
    Feb 3, 2010
  5. alex
    Replies:
    3
    Views:
    559
    Richard Cornford
    Dec 28, 2006
Loading...

Share This Page