473,497 Members | 2,184 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
Nov 14 '05 #1
4 2282

"Office Drone" <fu**********@yahoo.com> wrote in message

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.
Nov 14 '05 #2
In article <68**************************@posting.google.com >,
Office Drone <fu**********@yahoo.com> wrote:
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
Nov 14 '05 #3
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
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++.
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org
Nov 14 '05 #4
Ben Pfaff <bl*@cs.stanford.edu> wrote in message news:<87************@benpfaff.org>...
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
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++.


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 );

--------------

that bombs out with an error
'free' : ambiguous call to overloaded function
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

88
5047
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from...
1
3280
by: Zenobia | last post by:
Hello I want a search & replace text in source code for several files in several directories. It would seem that both Dreamweaver MX 6 and GoLive 6 offer this feature but not for .aspx file...
8
2507
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
8
2610
by: UDBDBA | last post by:
All: Anyone had bad experience with doing DECLARE DGTT "WITH REPLACE" option? The suggested workaround was to do "DELETE FROM DGTT" sql statement. We are seeing DGTT performance issue when...
38
2242
by: MLH | last post by:
I have 2 global constants declared in the same global module: Global Const MY_VERSION$ = "1.04" Global Const ProjectName$ = "MyProj" I have 2 global procedures designed to return the value of...
4
1757
by: Simon Harris | last post by:
Hi All, I'm new to ASP.Net, so be gentle! (Plenty of 'classic' ASP experience), just one question... - Am I correct in thinking that global functions are stored in ASCX files? Thanks! ...
18
2923
by: robert | last post by:
Using global variables in Python often raises chaos. Other languages use a clear prefix for globals. * you forget to declare a global * or you declare a global too much or in conflict * you...
37
2695
by: eoindeb | last post by:
Sorry to ask another global variable question, but from reading other posts I'm still not sure whether to use them or not. I have a program with a set function that calls 4 other functions in...
5
9199
by: V S Rawat | last post by:
I was trying to use back-to-back replace functions to convert a url: str1 = str.replace("%2F","/").replace("%3F","?").replace("%3D","=").replace("%2 6","&"); It didn't replace all 4 types of...
0
6991
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7196
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7373
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4897
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1405
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
286
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.