473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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++.moderated. First time posters: Do this! ]
Jul 23 '05 #1
6 5849
shoosh <sh********@walla.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++.moderated. 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.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. 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" ,++cBlocksAllocated,
stAllocateBlock);
return NULL;
}
// User-defined operator delete.
void operator delete( void *pvMem )
{
printf("Memory block %d deallocated\n", --cBlocksAllocated);
}

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.local...
shoosh <sh********@walla.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++.moderated. First time posters: Do this! ]
Jul 23 '05 #4

"shoosh" <sh********@walla.co.il> wrote in message
news:ct**********@news2.netvision.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++.moderated. 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++.moderated. 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.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. 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
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...
1
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...
0
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...
3
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...
1
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...
2
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...
9
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...
7
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;...
1
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 {...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
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
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...
0
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...

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.