473,800 Members | 2,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overriding global new and delete + STL

hi
for my application under VC6 I need to implement my own memory manager
which overrides the global new and delete operators and which Do Not
use the normal free() and malloc().
it seemed to work fine on its own but when I tried to use say 'cout' or
'cerr' from STL
the tester crashed upon termination.
using some breakpoints I found out that when I make use of 'cerr' in the
program, there
is a call to my override of delete which do not correspond to any call to my
override to new. infact, with just a main() saying:
int main() {
cerr << "boo";
return 0;
}
all I got was a break in my operator delete and no breaks at all in my
operator new.
how is the memory allocated if now by using my operator new? where does this
pointer come from?

does anyone know anything about this issue? any help?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 23 '05 #1
6 5895
shoosh <sh********@wal la.co.il> writes:
using some breakpoints I found out that when I make use of 'cerr' in the
program, there
is a call to my override of delete which do not correspond to any call to my
override to new. infact, with just a main() saying:
int main() {
cerr << "boo";
return 0;
}


Please post the entire program, including the overloaded
(de)allocation functions.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 23 '05 #2
shoosh wrote:
how is the memory allocated if now by using my operator new?


I'm a little bit rusty on replacing operators new and operator delete
but I could imagine that the memory is allocated with the 'nothrow'
version of 'operator new()' but deleted with the normal 'operator
delete()'. That is, you might want to add the following function:

/**/ void operator new(std::size_t sz, std::nothrow const&) throw()
/**/ {
/**/ ...
/**/ }

A brief glance at the standard just states that both version of
'operator new()' are replaceable but there is no statement that they
need to be replaced together. Of course, this makes for an interesting
problem: as an implementer of the standard C++ library I would probably
implement the throwing version in terms of the not throwing one. Of
course, if a user replace the throwing version, 'operator delete()' may
get called with memory not allocated from his replacement version
because the nothrow version is called somewhere. Since there is no way
to detect whether the not throwing one is replaced, there is no chance
to do it correctly: even if the operators are implemented the other way
around things could break. All the user needs to do is to replace the
'operator new()'. Probably the standard is defective if it does not
state that both versions of 'operator new()' need to be replaced if one
is replaced.
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jul 23 '05 #3
the whole new Memory manager I wrote it not relevant to the discussion and
is too big to post.
yet the problem I describe can be recreated simply, like so:

#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
void *operator new( size_t stAllocateBlock )
{
printf("Memory block %d allocated for %d bytes\n" ,++cBlocksAlloc ated,
stAllocateBlock );
return NULL;
}
// User-defined operator delete.
void operator delete( void *pvMem )
{
printf("Memory block %d deallocated\n", --cBlocksAllocate d);
}

int main( int argc, char *argv[] )
{
cout << "YEA!" << endl;
return 0;
}
running this code results in the following output:
YEA!
Memory block -1 deallocated
Memory block -2 deallocated

which means the my override of delete ran twice and the override of new did
not ran at all.
if I loose the 'cout' line and the #include <iostream.h> the output is
nothing.
"Thomas Maeder" <ma****@glue.ch > wrote in message
news:m2******** ****@madbox2.lo cal...
shoosh <sh********@wal la.co.il> writes:
using some breakpoints I found out that when I make use of 'cerr' in the
program, there
is a call to my override of delete which do not correspond to any call to
my
override to new. infact, with just a main() saying:
int main() {
cerr << "boo";
return 0;
}


Please post the entire program, including the overloaded
(de)allocation functions.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 23 '05 #4

"shoosh" <sh********@wal la.co.il> wrote in message
news:ct******** **@news2.netvis ion.net.il...
the whole new Memory manager I wrote it not relevant to the discussion and
is too big to post.
yet the problem I describe can be recreated simply, like so:

#include <stdio.h>
#include <iostream.h>


Use <iostream>, not <iostream.h>.

<iostream.h> is non-standard, so there is no telling what this thing does
for some cases.

<iostream> is the standard header, and will exhibit (or should exhibit)
standard-conforming behavior. Once you do this, you have to remember that
cout is now in the std namespace, and you must adjust your code accordingly.

Paul McKenzie
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 23 '05 #5

Dietmar Kuehl wrote:
A brief glance at the standard just states that both version of
'operator new()' are replaceable but there is no statement that they
need to be replaced together. Of course, this makes for an interesting problem: as an implementer of the standard C++ library I would probably implement the throwing version in terms of the not throwing one. Of
course, if a user replace the throwing version, 'operator delete()' may get called with memory not allocated from his replacement version
because the nothrow version is called somewhere. Since there is no way to detect whether the not throwing one is replaced, there is no chance to do it correctly: even if the operators are implemented the other way around things could break. All the user needs to do is to replace the
'operator new()'. Probably the standard is defective if it does not
state that both versions of 'operator new()' need to be replaced if

one

A footnote would be nice, but if I replace one to forward to the other,
I would like it to work. This logically works (when the allocation
succeeds, the same value is returned and delete doesn't care how it
got that value) but your extra wording would prohibit it. Not sure
if there's a good reason to do so, but it would work as long as
any implementor is reasonably careful.

The tricky point for implementors is that if the built-in throwing
unconditionally forwards to the non-throwing, and the user replaces
only that one to forward to the throwing, you end up with a loop. So
an implementor probably has to call some shared __new() (or malloc())
from both versions of the default operator new.

With this implementation, replacing either version of new to forward
to the other will work ok, including deletion.

Regards,
Michiel Salters
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 23 '05 #6
shoosh wrote:
#include <iostream.h>


I don't know whether it is related to your problem but you should get
rid
of this header as soon as possible: it is non-standard. You should use

/**/ #include <iostream>

(i.e. without the ".h") instead.

Other than that, I would guess that you should also replace the
"nothrow"
version of 'operator new()'.
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 23 '05 #7

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

Similar topics

1
2315
by: Jesper Madsen | last post by:
Can I confine overrides of new and delete to certain namesspaces, f.ex. by just introducing the modfied operator new in namespace GUI?? Or should I ask, if I declare operator new in a namespace, is it still global operator new I define?? This is just for doing some memory tracking, so it would be nice if I could keep namespace std:: out of the game.. and avoid STL. I do not have any using std namespace; or such in my code, so this will...
1
3849
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there are quite a few things that I learned that I did not know before. For example, while I knew that the new and delete operators can be overloaded for classes, I did not know that that the global new and delete operators can also be overloaded. ...
0
1130
by: Ryan Mack | last post by:
I'm doing development on an embedded system using a GCC 2.96 MIPS cross compiler and a minimal C standard library replacement. The system loads at startup a base executable. The base executable then loads one DLL after another during execution to reduce memory usage. The base executable statically links in a library which overrides the default malloc (and related functions) and the built-in operator new/delete. That works fine and as...
3
7804
by: Cheng Mo | last post by:
When overriding operator new & delte of one class, the method is implicitly declared as static. However, overriding operator new & delete of template cannot be static.The compiler says cannot declare the function a static linkage. Why? C++ is so complex and so many situation should be considered.
1
1936
by: Anders Borum | last post by:
Hello! I have a framework where all objects are uniquely identified by a GUID (Global Unique Identifier). The objects are used in conjunction with lots of hashtables and I was thinking, that overriding the GetHashCode base method (inherited from object) was a good idea. public abstract class CmsObjectNode : CmsObject, IXml { private Guid cmsObjectID = Guid.NewGuid();
2
2026
by: byoukstetter | last post by:
So, I have an interface with several overriding methods: using System; using System.Collections.Specialized; namespace some.name.space { public interface IVrsPersistenceProvider { string Select(string idName, string tableName, string whereClause);
9
10285
by: groleo | last post by:
Hi list. Simple question: is it possible to override the global new/delete operators, without using malloc/free? I mean something in the ideea of the code below, which doesnt work cause of the recursivenes. What I'm trying to say is how to override new/delete using the original global
7
6650
by: Lighter | last post by:
Is overriding a function of a library in accordance with C++ standard? The following code are passed by the VS 2005 and Dev C++. #include <cstdlib> #include <iostream> using namespace std; size_t strlen(const char* p)
1
11451
by: SarahT | last post by:
Hi folks, I am doing something Very Bad and Wrong (which I'll spare you the details of) that requires overloading new for some specific classes. So, for example: class MyWeirdThingy { public: ...
0
9690
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
10501
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10273
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...
0
9085
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...
1
7574
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5469
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
5603
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
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
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.