473,387 Members | 1,545 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,387 software developers and data experts.

global operator new

hi,
i want to redefine the global operator new for logging
and stats purposes. is it a good idea to use malloc()
as allocation function ? or should i call one of those
weird crt functions....or ask the OS directly?

oh and another question: which parts of my program will
actually use my version of new ? only the ones that include
the declaration (do i even need one or is a definition in a
cpp file ok?). what about statically linked libs, and what
about the C++ standard libs? what about dlls?
will they all use the new new ? ;)

thanks
martin
Jul 22 '05 #1
7 2136
Martin Stich wrote:
hi,
i want to redefine the global operator new for logging
and stats purposes. is it a good idea to use malloc()
as allocation function ? or should i call one of those
weird crt functions....or ask the OS directly?

oh and another question: which parts of my program will
actually use my version of new ? only the ones that include
the declaration (do i even need one or is a definition in a
cpp file ok?). what about statically linked libs, and what
about the C++ standard libs? what about dlls?
will they all use the new new ? ;)


The right answer to this question is "implementation defined".

Each C++ compiler implementation has different answers to your question
(even different versions of the same compiler).

malloc is an excellent place to trap memory allocation calls but for C++
a compiler is able to "inline" new operations and so requests that end
up in malloc may or may not be truly "allocated" as a C++ object.

However, to be friendly to leak detection libs, some C++ implementations
allow you to substitute the regular allocator for a simple malloc
allocator. e.g. in the latest gcc compiler, you can simply set an
environment variable and "viola" you get malloc called for every new and
free for every delete (for classes using the regular gcc allocator).

Jul 22 '05 #2
On Fri, 28 Nov 2003 03:21:13 +0100, "Martin Stich"
<ms****@primusnetz.de> wrote:
hi,
i want to redefine the global operator new for logging
and stats purposes. is it a good idea to use malloc()
as allocation function ?
Yes, it has the right guarantees about alignment, etc., and is easy to
use and standard.

or should i call one of thoseweird crt functions....or ask the OS directly?
Only call something else if you have special performance requirements,
etc.
oh and another question: which parts of my program will
actually use my version of new ? only the ones that include
the declaration (do i even need one or is a definition in a
cpp file ok?).
Since the declaration is exactly the same as the standard global
operator new, including it makes no difference.

what about statically linked libs, and whatabout the C++ standard libs? what about dlls?
will they all use the new new ? ;)


Probably not. Statically linked libs probably will, dlls probably
won't, but this is platform dependent. i.e. the results will probably
be different with .so's under Linux and .dll's under Windows.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #3

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bq********@dispatch.concentric.net...
Martin Stich wrote:
hi,
i want to redefine the global operator new for logging
and stats purposes. is it a good idea to use malloc()
as allocation function ? or should i call one of those
weird crt functions....or ask the OS directly?

oh and another question: which parts of my program will
actually use my version of new ? only the ones that include
the declaration (do i even need one or is a definition in a
cpp file ok?). what about statically linked libs, and what
about the C++ standard libs? what about dlls?
will they all use the new new ? ;)


The right answer to this question is "implementation defined".

Each C++ compiler implementation has different answers to your question
(even different versions of the same compiler).

malloc is an excellent place to trap memory allocation calls but for C++
a compiler is able to "inline" new operations and so requests that end
up in malloc may or may not be truly "allocated" as a C++ object.

[SNIP]

Hi Gianni,
could you please elaborate on the "may or may not" part. If I understand you
correctly you indicate that due to the use of malloc inside ones own
implementation of new it is not for sure that the object is constructed. In
my experience the following code worked just fine also for objects, but I
don't wanna claim to be an export on implementations of memory allocators.

void* operator new(size_t Bytes, const char* pFile , int Line )
{
return CMemObserver::GetRef().Allocate( Bytes, pFile, Line );
};

void* CMemObserver::Allocate( size_t Bytes, const char* pFile, int Line ) {
void* pPtr = malloc( Bytes);
if( !pPtr )
throw std::bad_alloc(); // ANSI/ISO compliant behavior
m_MemoryMap[pPtr] = CNewData( pFile, Line);
return pPtr;
}

Any further insight or hints for improvements are more than welcome.

Cheers
Chris
Jul 22 '05 #4
Chris Theis wrote:
....

Hi Gianni,
could you please elaborate on the "may or may not" part. If I understand you
correctly you indicate that due to the use of malloc inside ones own
implementation of new it is not for sure that the object is constructed. In
my experience the following code worked just fine also for objects, but I
don't wanna claim to be an export on implementations of memory allocators.

void* operator new(size_t Bytes, const char* pFile , int Line )
{
return CMemObserver::GetRef().Allocate( Bytes, pFile, Line );
};

void* CMemObserver::Allocate( size_t Bytes, const char* pFile, int Line ) {
void* pPtr = malloc( Bytes);
if( !pPtr )
throw std::bad_alloc(); // ANSI/ISO compliant behavior
m_MemoryMap[pPtr] = CNewData( pFile, Line);
return pPtr;
}

Any further insight or hints for improvements are more than welcome.


"may or may noy" refers to how some compiler implementations define
opertor new to have a "caching" behaviour. Have a look at the regular
new operator for the latest GCC C++ compiler - if you have libraries
that use this allocator, defining your own allocator will not change the
allocator in the library.

Also, if a class defined it's own new operator, your global new operator
will not be used.

Hence, I think the best way to gets stats on object level memory
allocation is implementation dependant, in GCC for example, the best
mechanism is to substitute malloc/free etc and to set the magic
environment variable GLIBCXX_FORCE_NEW (or GLIBCPP_FORCE_NEW depending
on version).

See:
http://gcc.gnu.org/onlinedocs/libstd...t/howto.html#3

The solution you propose is fine if you only care about the classes in
modules you have control over and you need to be careful that the
allocation of an object uses the corresponding deallocator for
destruction. This is not allways true when you have "standard" objects
being allocated and deallocated in different modules. "namespaces" may
come to the rescue here, I'm not sure but it seems likely that an
"global" new operator defined in a namespace will only apply to classes
in that namespace.

However, if you're looking at leak detection or "stats", usually you're
looking for results from the entire application hence finding a way to
trap all dynamic construction events is the ultimate goal. The nice
thing about trapping malloc/free is that it also traps regular C
libraries as well and hence mixed C/C++ applications are also caught.
Jul 22 '05 #5

gianni,
thanks for your detailed tips!
so if i understand this correctly, in order to trap _all_ allocs of
a program, even the ones from static (and dynamic - is this even
possible?) libs, malloc/realloc/calloc/free instead of the new/delete
operators should be replaced ? or both (if so, do i have to define my
own placement new, too?) ? how would i replace malloc etc...those
are macros, right ? so would i #undef them and then redefine them..
to what ? and how would external libs know i replaced the macros,
since they dont include my code ?
since i use vc++ and not gcc, unfortunately the url doesnt help me
a lot. how can i assure i am really trapping all allocs, and didnt miss
one...is there a way of testing this ?
sorry these are so many new questions - didnt know the subject was
that complex ;)

thanks
martin

Jul 22 '05 #6
Martin Stich wrote:
gianni,
thanks for your detailed tips!
so if i understand this correctly, in order to trap _all_ allocs of
a program, even the ones from static (and dynamic - is this even
possible?) libs, malloc/realloc/calloc/free instead of the new/delete
operators should be replaced ? or both (if so, do i have to define my
own placement new, too?) ? how would i replace malloc etc...those
are macros, right ? so would i #undef them and then redefine them..
to what ? and how would external libs know i replaced the macros,
since they dont include my code ?
since i use vc++ and not gcc, unfortunately the url doesnt help me
a lot. how can i assure i am really trapping all allocs, and didnt miss
one...is there a way of testing this ?
sorry these are so many new questions - didnt know the subject was
that complex ;)


I've not had that much success doing what you ask with visual studio (I
have not really tried that hard).

The point I was trying to make is that this is platform specific and
really off-topic for this news-group. You could try one of the
microsoft visual studio news groups, I'm sure you'll get a much better
response that I can give you.

Jul 22 '05 #7

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bq********@dispatch.concentric.net...
Chris Theis wrote:
... [SNIP] However, if you're looking at leak detection or "stats", usually you're
looking for results from the entire application hence finding a way to
trap all dynamic construction events is the ultimate goal. The nice
thing about trapping malloc/free is that it also traps regular C
libraries as well and hence mixed C/C++ applications are also caught.


Cheers for the information!

Chris
Jul 22 '05 #8

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

Similar topics

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...
7
by: Emanuel Ziegler | last post by:
Hello, I want to do some mathematics with functions. In my case the function classes are very complex, but this simple example has the same problems. To allow calculations that begin with a...
44
by: Mohanasundaram | last post by:
int i = 10; int main() { int i = 20; return 0; } Hi All, I want to access the global variable i inside the main. Is there
9
by: Javaman59 | last post by:
I saw in a recent post the :: operator used to reach the global namespace, as in global::MyNamespace I hadn't seen this before, so looked it up in MSDN, which explained it nicely. My question...
16
by: Roman Ziak | last post by:
Hello, there were times when I used to be looking for a way to access JavaScript Global object similar to those found in VBScript or PHP ($GLOBALS). At present this has only academic value for...
2
by: ryan_melville | last post by:
Hi, Should I put the operator<<() for my class (which is in a namespace) in the namespace or make it global? If I understand the lookup rules correctly: If I make it global, it may be hidden...
7
by: Eric Lilja | last post by:
>From a book, I know the following is true for the comparison operators: An overloaded operator that is a class member is only considered when the operator is used with a *left* operand that is an...
3
by: mrstephengross | last post by:
Hi folks. I've got a weird situation--gcc doesn't like the folllowing code snippet, but I don't know if it's correct or not. Here's the situation: In the global namespace, I've got a operator<<...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
1
by: google | last post by:
We need to be able to disable global operator new at compile time. We are developing a large library and need to control all memory allocation and cannot have a mistake whereby a mistaken use of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...

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.