473,614 Members | 2,508 Online
Bytes | Software Development & Data Engineering Community
+ 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(dbghe ap.obj) : error LNK2005: _free already defined in
mysrcfile.obj
Nov 14 '05 #1
4 2291

"Office Drone" <fu**********@y ahoo.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**********@y ahoo.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.stanfor d.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", cBlocksAllocate d--);
::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
5111
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 the days before PHP got object-oriented features. For instance we currently have:
1
3288
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 types. Is there a hack that sets the configuration files for one of these programs and fools the program into allowing the global search and replace?
8
2530
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 in global space, therefore it is not a global variable, yes? Even if it was global, how would it get from one function to another? In PHP variables are copied by value. Are they copied by reference in Javascript? <SCRIPT LANGUAGE="JavaScript">
8
2628
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 replacing existing DGTT while declaring it. Which way is better?
38
2276
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 these 2 constants... Function GetCurrentRev() As String GetCurrentRev = MY_VERSION$ End Function
4
1767
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! Simon.
18
2937
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 have a local identical variable name and want to save/load it to/from the global with same name * while you add code, the definition of globals moves more and more apart from their use cases -> weirdness; programmers thinking is fragmented * using...
37
2727
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 order - let's say function A, B, C, D. It always calls function A first which is a function that returns a system path. Now all other functions require that variable as well (function A returns a char pointer)
5
9247
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 strings. Then, I googled and found this suggestion of some JavaScript Tutorials, so I used replace with a regex with a global switch:
0
8198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8591
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8294
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5549
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4058
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4138
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2575
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 we have to send another system
0
1438
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.