isolating operator new overload with gcc 4 (XCode2.2)

R

richard.parker

Hello,

I need to overload operator new with affecting the system libraries.
Has anyone done this? I've got 2 static libraries and application
source code where the operator needs to be overloaded, but I need to
link with the system libraries (frameworks) and I DO NOT want my
overloads to be mapped to them. I'm working with some code that is
currently working on Win32 but also needs to work on the Mac. Under
windows this is very simple because none of the essential Win32
libraries use or require C++ (or at least in no way that gets exposed
to normal application code), and overloading operator new has no effect
on their memory allocations. I need similar functionality on the Mac
using XCode2.2 (which I think is using gcc 4). This first pass is just
being built for intel (mainly so I don't have to deal with endian
issues right now). Maybe some odd combination of dynamic and static
libraries linked in the right order might do it??
 
R

richard.parker

Hello,

I need to overload operator new with affecting the system libraries.
Has anyone done this? I've got 2 static libraries and application
source code where the operator needs to be overloaded, but I need to
link with the system libraries (frameworks) and I DO NOT want my
overloads to be mapped to them. I'm working with some code that is
currently working on Win32 but also needs to work on the Mac. Under
windows this is very simple because none of the essential Win32
libraries use or require C++ (or at least in no way that gets exposed
to normal application code), and overloading operator new has no effect
on their memory allocations. I need similar functionality on the Mac
using XCode2.2 (which I think is using gcc 4). This first pass is just
being built for intel (mainly so I don't have to deal with endian
issues right now). Maybe some odd combination of dynamic and static
libraries linked in the right order might do it??

Just a correction for the top... I need to overload new WITHOUT
affecting the system libraries (frameworks).
 
F

Fred Zwarts

Just a correction for the top... I need to overload new WITHOUT
affecting the system libraries (frameworks).


This is not really a C++ question, more an system specific C++ implementation question.
However, I tried to do something like you asked, but with limited success.

It is not clear what you want. Do you want the C++ Standard Library to use your overloaded new,
or do you want them to use the original new?

If you want to use your overloaded new everywhere, then use static libraries only, because
operator new is used in many places in the standard library.
If you want to use your overloaded new for your own code and the original new for the STL,
then your are moving on dangerous grounds. There is not a clear separation of the two,
because a large part of the STL consists of templates. Do the specific instantiations of those
templates belong to the STL, or to your own code? Some of those instantiations are used also
in the STL and, therefore, will be in the dynamic C++ library, others are only in your own code
and will not be in a dynamic library. You have no control over which instantiations are in
the dynamic library and which are not. So, you have no control over when your instantiations
use your overloaded new and when they use the original new.

If you still insist on doing so, try to link the object code of your program with the object code of the
overloaded operator new and then add the normal libraries to the linker command.

Fred.Zwarts.
 
R

richard.parker

Fred said:
This is not really a C++ question, more an system specific C++ implementation question.
However, I tried to do something like you asked, but with limited success.

It is not clear what you want. Do you want the C++ Standard Library to use your overloaded new,
or do you want them to use the original new?

If you want to use your overloaded new everywhere, then use static libraries only, because
operator new is used in many places in the standard library.
If you want to use your overloaded new for your own code and the original new for the STL,
then your are moving on dangerous grounds. There is not a clear separation of the two,
because a large part of the STL consists of templates. Do the specific instantiations of those
templates belong to the STL, or to your own code? Some of those instantiations are used also
in the STL and, therefore, will be in the dynamic C++ library, others are only in your own code
and will not be in a dynamic library. You have no control over which instantiations are in
the dynamic library and which are not. So, you have no control over when your instantiations
use your overloaded new and when they use the original new.

If you still insist on doing so, try to link the object code of your program with the object code of the
overloaded operator new and then add the normal libraries to the linker command.

Fred.Zwarts.

Sorry about the ambiguity, my goal is to overload new/delete for all of
my code and the STL that my code is using. Basically everything except
the system libraries (frameworks in XCode, which I believe are just
dynamic libraries, linked with a dynamic c runtime). Is it possible,
with gcc 4, to isolate operator new/delete overloads to a libarary?
Can it be static or does it have to be dynamic? If so I could put all
my code in one (library). But I don't want the system frameworks
(other dynamic libaries) to get a hold of my overloads when I link the
application and I don't want my code to use the non-overloaded
new/delete.
 
F

Fred Zwarts

Sorry about the ambiguity, my goal is to overload new/delete for all of
my code and the STL that my code is using. Basically everything except
the system libraries (frameworks in XCode, which I believe are just
dynamic libraries, linked with a dynamic c runtime). Is it possible,
with gcc 4, to isolate operator new/delete overloads to a libarary?
Can it be static or does it have to be dynamic? If so I could put all
my code in one (library). But I don't want the system frameworks
(other dynamic libaries) to get a hold of my overloads when I link the
application and I don't want my code to use the non-overloaded
new/delete.

1) I don't know XCode, So I am not sure that I completely understand your question.
I can speak only in general terms, which is applicable not only to gcc 4,
but also for other compiler environments.
My point is that it is not possible to make a clear separation of the STL that
your code is using and the STL that is used by libraries used by your code.
There is an overlap.
E.g., you may use the std::string class, but the library may use the std::string class
as well. The std::string class uses new/delete internally. Should this class std::string
use the overloaded version or the original version? You cannot have both.

2) You now speak not only about an overloaded operator new, but also about an
operator delete. If the overloaded versions are not compatible with the original
operators, the pairing of new and delete must be very strict. (An object created with
and overloaded new should not be destructed with the original delete and vice versa.)
This can not be guaranteed if part of the STL is in dynamic libraries and part of it is
in static libraries. However, as I explained before, you have no control of what is
in dynamic libraries and what is in static libraries, in particular in cases where templates
from the STL are used.

Concluding I would say that what you want is impossible in interpreted strictly.
However, if you can live with cases where the wrong version of the new/delete is
used it is possible to go a bit in that direction.

Fred.Zwarts.
 
T

toton

Hello,

I need to overload operator new with affecting the system libraries.
Has anyone done this? I've got 2 static libraries and application
source code where the operator needs to be overloaded, but I need to
link with the system libraries (frameworks) and I DO NOT want my
overloads to be mapped to them. I'm working with some code that is
currently working on Win32 but also needs to work on the Mac. Under
windows this is very simple because none of the essential Win32
libraries use or require C++ (or at least in no way that gets exposed
to normal application code), and overloading operator new has no effect
on their memory allocations. I need similar functionality on the Mac
using XCode2.2 (which I think is using gcc 4). This first pass is just
being built for intel (mainly so I don't have to deal with endian
issues right now). Maybe some odd combination of dynamic and static
libraries linked in the right order might do it??
Do not overload the global new operator. Over the new operator specific
to your class only, if you at all need.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top