How can I replace global free() and xalloc() functions?

O

Office Drone

I already figured out how to replace new & delete operators, which
wasn't that hard and this is working great globally.

However, aside from new & delete, memory allocation is also used via
calling malloc/calloc & free functions.

What is the proper way to override these functions globally, not from
inside the class?

When I simply replace it like:

file: mysrcfile.c
----------------------------------------------
void free( void *memblock ) {
static fGuard = 0;
if( !fGuard )
{
fGuard = 1;
printf("free: block count=%i\n", --nMemBlocks);
fGuard = 0;
}

::free( memblock );
}
 
M

Malcolm

Office Drone said:
However, aside from new & delete, memory allocation is also used via
calling malloc/calloc & free functions.

What is the proper way to override these functions globally, not from
inside the class?
There's no way, which is one reason why you should always use new / delete
when writing C++.

What you have to do is write your own mymalloc() / myfree() and then trawl
through the code looking for calls. I have done this myself for large
programs, generally it isn't such a big job.
 
R

Richard Tobin

Office Drone said:
However, aside from new & delete, memory allocation is also used via
calling malloc/calloc & free functions.

What is the proper way to override these functions globally, not from
inside the class?

It sounds like you're using C++, but - since you posted in comp.lang.c -
there's no portable way to do it in C.

Your linker may have a way to resolve the conflict, but even if it
does this may not work if other library functions rely on the internal
workings of malloc.

You could #define malloc (etc) to be my_malloc (etc) in each file
(*after* the standard headers, and you should also #undef malloc
first, in case <stdlib.h> defined it as a macro). This will of course
have no effect on calls to malloc from other library functions.
(Language lawyers: is this legal?)

-- Richard
 
B

Ben Pfaff

You could #define malloc (etc) to be my_malloc (etc) in each file
(*after* the standard headers, and you should also #undef malloc
first, in case <stdlib.h> defined it as a macro). This will of course
have no effect on calls to malloc from other library functions.
(Language lawyers: is this legal?)

Not in C. Standard library function names are reserved.
Defining a reserved identifier as a macro name yields undefined
behavior. See C99 7.1.3#2. There is a special dispensation for
*un*defining certain reserved identifiers, but no corresponding
exception for defining them.

I can't speak for C++.
 
J

John Harrison

I already figured out how to replace new & delete operators, which
wasn't that hard and this is working great globally.

However, aside from new & delete, memory allocation is also used via
calling malloc/calloc & free functions.

What is the proper way to override these functions globally, not from
inside the class?

When I simply replace it like:

file: mysrcfile.c
----------------------------------------------
void free( void *memblock ) {
static fGuard = 0;
if( !fGuard )
{
fGuard = 1;
printf("free: block count=%i\n", --nMemBlocks);
fGuard = 0;
}

::free( memblock );
}
-----------------------------------------------

I get a link error:
LIBCD.lib(dbgheap.obj) : error LNK2005: _free already defined in
mysrcfile.obj

There is no portable way to replace free, malloc etc.

In any case your definition of free has a recursive call to itself. You
might find it easier to replace free if you don't try to also use the free
you are replacing.

In any case VC++ already has heap debugging techniques that do what you
seem to be trying to do and a whole lot more. Search for 'The CRT debug
heap' in MSDN.

john
 
O

Office Drone

Ben Pfaff said:
Not in C. Standard library function names are reserved.
Defining a reserved identifier as a macro name yields undefined
behavior. See C99 7.1.3#2. There is a special dispensation for
*un*defining certain reserved identifiers, but no corresponding
exception for defining them.

I can't speak for C++.

I could limit the code to C++ notation (since I will be using classes
anyway), so the redefinition via #define sounds like a good solution.

I also tried to use a namespace (which is also C++ - specific, as far
as I understand), but the following crashes during the compilation:

namespace {
void free( void *memblock ) {
static fGuard = 0;
if( !fGuard )
{
fGuard = 1;
printf("free: block count=%i\n", cBlocksAllocated--);
::free( memblock );
printf("freed: %p\n", memblock);
fGuard = 0;
} //if
} // free
} // unnamed namespace

void* iPtr = calloc( 1, 4096 );
free ( iPtr );
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top