Q: address of operator new/delete

R

Rob Williscroft

Jakob Bieling wrote in
Hi,

is it possible to get the address of operator new/delete and
operator
new[]/delete[] portably? Otherwise I would have to go for a
non-portable and rather unclean solution.

A brute force approach:

#include <iostream>
#include <ostream>
#include <cstring>
#include <iomanip>

struct function_key
{
template < typename T >
function_key( T *func )
{
void (*temp)() = reinterpret_cast< void (*)() >( func );
std::memcpy( buf, &temp, sizeof( buf ) );
}

bool operator <( function_key const &rhs ) const
{
return std::memcmp( buf, rhs.buf, sizeof( buf ) ) < 0;
}
// etc ...

friend std::eek:stream &operator << (
std::eek:stream &os, function_key const &f
);

private:

unsigned char buf[ sizeof( void (*)() ) ];
};


std::eek:stream &operator << ( std::eek:stream &os, function_key const &f )
{
// insert code to save os's state ...
os
<< std::hex
<< std::setprecision(2)
<< std::setfill('0')
<< std::uppercase
;

for ( std::size_t i = sizeof( f.buf ); i; )
{
os << std::setw(2) << unsigned( f.buf[ --i ] );
}

// restore os..
return os;
}

void test( int )
{
}

int main()
{
std::cout
<< function_key(
static_cast< void *(*)(std::size_t) >( operator new )
)
<< std::endl
;

std::cout
<< function_key(
static_cast< void *(*)(std::size_t, void *) >( operator new )
)
<< std::endl
;

std::cout << function_key( test ) << std::endl;
}


Rob.
 

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
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top