Hi
I am working on a module of a big project. For some reason, I have to
overload operator new and delete in my module. However, when building
the whole project, I do not want to expose my overloaded new and delete
to other modules. ( my module is a static library .a file when we build
the whole project) In other words, I want the overloaded new and delete
only works in my files of my module.
Any good ideas?
Thanks
yan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
shahehe (6)
|
4/29/2006 10:25:03 AM |
|
In article <1146201959.399133.176840@i39g2000cwa.googlegroups.com>,
shahehe@gmail.com writes
> >Hi
> >
> >I am working on a module of a big project. For some reason, I have to
> >overload operator new and delete in my module. However, when building
> >the whole project, I do not want to expose my overloaded new and delete
> >to other modules. ( my module is a static library .a file when we build
> >the whole project) In other words, I want the overloaded new and delete
> >only works in my files of my module.
Then just overload them. For how to do that, have a look at the way the
Standard provides the nothrow version of new. However If you seriously
want to provide a version for delete that is not a class member I think
you have a real problem.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions:
http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Francis
|
4/30/2006 2:57:12 PM
|
|
shahehe@gmail.com wrote:
> > Hi
> >
> > I am working on a module of a big project. For some reason, I have to
> > overload operator new and delete in my module. However, when building
> > the whole project, I do not want to expose my overloaded new and delete
> > to other modules. ( my module is a static library .a file when we build
> > the whole project) In other words, I want the overloaded new and delete
> > only works in my files of my module.
> >
> > Any good ideas?
Do not overload global new/delete then, because doing so you violate
One Definition Rule.
Use factory functions or overload new/delete for your classes only. Use
a custom allocator for your containers.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Maxim
|
4/30/2006 2:59:29 PM
|
|
In article <1146387034.353695.159970@y43g2000cwc.googlegroups.com>,
Maxim Yegorushkin <maxim.yegorushkin@gmail.com> writes
>
>shahehe@gmail.com wrote:
>>> Hi
>>>
>>> I am working on a module of a big project. For some reason, I have to
>>> overload operator new and delete in my module. However, when building
>>> the whole project, I do not want to expose my overloaded new and delete
>>> to other modules. ( my module is a static library .a file when we build
>>> the whole project) In other words, I want the overloaded new and delete
>>> only works in my files of my module.
>>>
>>> Any good ideas?
>
>Do not overload global new/delete then, because doing so you violate
>One Definition Rule.
I think you are confusing overloading with replacement. C++ allows the
replacement of the global operator new and operator delete (which is
useful when wanting to track memory leaks) but it also allows
overloading operator new. In addition to the overloads provided by the
placement version and the 'nothrow' version users can add their own
overloads.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Francis
|
4/30/2006 5:42:21 PM
|
|
>> Donot overload global new/delete then, because doing so you violate
>> One Definition Rule.
>> Use factory functions or overload new/delete for your classes
>> only. Use
>> a custom allocator for your containers.
The problem is that in my module, I have call a mathematical package
frequently and that the code of that package has new/delete. I do not
want to change the code in that package, so I decide to overload global
new/delete. On the other hand, as I mentioned, I do not want my
new/delete affects other the code in other moduel. so what should I do?
thanks again
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
shahehe
|
5/1/2006 3:36:00 PM
|
|
In article <1146421440.696942.80100@e56g2000cwe.googlegroups.com>,
<shahehe@gmail.com> wrote:
> >> Donot overload global new/delete then, because doing so you violate
> >> One Definition Rule.
>
> >> Use factory functions or overload new/delete for your classes
> >> only. Use
> >> a custom allocator for your containers.
>
>
>
> The problem is that in my module, I have call a mathematical package
> frequently and that the code of that package has new/delete. I do not
> want to change the code in that package, so I decide to overload global
> new/delete. On the other hand, as I mentioned, I do not want my
> new/delete affects other the code in other moduel. so what should I do?
>
Outside of getting out of the C++ standard and using a client server
approach [your module provides a client to access the server that
executes the library] I can't think of a solution with the given
infromation.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Carl
|
5/2/2006 10:44:04 AM
|
|
shahehe@gmail.com wrote:
> The problem is that in my module, I have call a mathematical package
> frequently and that the code of that package has new/delete. I do not
> want to change the code in that package, so I decide to overload global
> new/delete. On the other hand, as I mentioned, I do not want my
> new/delete affects other the code in other moduel. so what should I do?
What's wrong with letting the mathematical package call new and
delete? Is there a performance problem?
Please explain what the *real* problem is.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Allan
|
5/2/2006 10:45:43 AM
|
|
Hi,
> The problem is that in my module, I have call a mathematical package
> frequently and that the code of that package has new/delete. I do not
> want to change the code in that package, so I decide to overload global
> new/delete. On the other hand, as I mentioned, I do not want my
> new/delete affects other the code in other moduel. so what should I do?
First a question: Do you really mean *overload*, i.e. add a new operator
new and operator delete of a different signature (i.e. different arguments),
or do you want to *replace* the existing operator new/delete pair, retaining
the arguments as in the original pair?
For the former case, nothing stops you. For the latter case, your best bet
is instead use a member operator new/delete pair. For that, construct a base
class that provides only these two, and derive your classes from this base
so they use the custom operator new/delete.
So long,
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Thomas
|
5/2/2006 10:51:11 AM
|
|
In article <1146421440.696942.80100@e56g2000cwe.googlegroups.com>,
shahehe@gmail.com writes
>>> Donot overload global new/delete then, because doing so you violate
>>> One Definition Rule.
>
>>> Use factory functions or overload new/delete for your classes
>>> only. Use
>>> a custom allocator for your containers.
>
>
>
> The problem is that in my module, I have call a mathematical package
> frequently and that the code of that package has new/delete. I do not
> want to change the code in that package, so I decide to overload
> global
> new/delete. On the other hand, as I mentioned, I do not want my
> new/delete affects other the code in other moduel. so what should I
> do?
You do not seem to be talking about overloading but of replacement of
the global versions. You cannot do that on a piecemeal basis, if you
choose to replace the global new and delete operators you must do so
everywhere in the program.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/
youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Francis
|
5/2/2006 7:51:39 PM
|
|
shahehe@gmail.com wrote:
>>>Donot overload global new/delete then, because doing so you violate
>>>One Definition Rule.
>
>
>>>Use factory functions or overload new/delete for your classes
>>>only. Use
>>>a custom allocator for your containers.
>
>
>
>
> The problem is that in my module, I have call a mathematical package
> frequently and that the code of that package has new/delete. I do not
> want to change the code in that package, so I decide to overload global
> new/delete. On the other hand, as I mentioned, I do not want my
> new/delete affects other the code in other moduel. so what should I do?
To have multiple different versions of the standared operator
new/delete, you need to violate the one definition rule. This is often
possibly if you partition your code into multiple independent modules,
such as DLLs. Then each DLL can have its own operator new replacement,
if it wants it.
Tom
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Tom
|
5/3/2006 8:02:51 PM
|
|
In article <e3a76m$hni$1@emma.aioe.org>, Tom Widmer
<tom_usenet@hotmail.com> writes
> shahehe@gmail.com wrote:
>>>> Donot overload global new/delete then, because doing so you violate
>>>> One Definition Rule.
>>
>>
>>>> Use factory functions or overload new/delete for your classes
>>>> only. Use
>>>> a custom allocator for your containers.
>>
>>
>>
>>
>> The problem is that in my module, I have call a mathematical package
>> frequently and that the code of that package has new/delete. I do not
>> want to change the code in that package, so I decide to overload
>> global
>> new/delete. On the other hand, as I mentioned, I do not want my
>> new/delete affects other the code in other moduel. so what should
>> I do?
>
> To have multiple different versions of the standared operator
> new/delete, you need to violate the one definition rule. This is often
> possibly if you partition your code into multiple independent modules,
> such as DLLs. Then each DLL can have its own operator new replacement,
> if it wants it.
You are still in breach of the one definition rule. Indeed it is one of
the major causes of problems with DLLs (breaches of the ODR) In addition
you better make sure that objects created in a module do not leak
anywhere else.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/
youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Francis
|
5/4/2006 8:49:19 PM
|
|
|
10 Replies
141 Views
(page loaded in 0.23 seconds)
Similiar Articles: Overloading operators new and delete - comp.lang.c++.moderated ...overload operator new in one module - comp.lang.c++.moderated ... C++ allows the replacement of the global operator new and operator delete (which is useful when wanting ... Overloading dereference pointers to pointers to class members ...Overloading dereference pointers to pointers to class members ... 12.7 ... SMART POINTERS: OVERLOADING OF ... via the new operator. X* p = new X( ... Non-member operator overloading, linker complains - comp.lang.c++ ...Non-member operator overloading, linker complains - comp.lang.c++ ... Problem is, that U ... pragmas or linker ... allocate memory from Windows, they call the operator new ... STL allocators, global new/delete using the heap and shared memory ...(where one cannot provide a new operator in the class). My gut feeling is that I must ... Overloading operators new and delete - comp.lang.c++.moderated ... STL allocators ... between-and operator - comp.soft-sys.sasOverloading operators new and delete - comp.lang.c++.moderated ... Dear experts, Please, consider the following example code: void * CLASS_A::operator new( size_t size ... sizeof implementation - comp.lang.cOverloading operators new and delete - comp.lang.c++.moderated ..... code: void * CLASS_A::operator new( size_t size ){ return Stack<sizeof(CLASS ... to be free after ... tdelete() example? - comp.lang.cOverloading operators new and delete - comp.lang.c++.moderated ... Dear experts, Please, consider the following example code: void * CLASS_A::operator new( size_t size ... Set implementation in STL - comp.lang.c++.moderated> AFAIK, yes, Red Black Trees are one of the most common implementations ... Overloading operators new and delete - comp.lang.c++.moderated ..... even though I'm not ... enum and operator++ - comp.lang.c++C++ - how to overload enum operator. . 1 Answers are available for this question. ... Sql - SqlSelect now can add individual fetch values using operator(). - New ... Why do pointer values display as ASCII characters instead of HEX ...The called overload of operator<< is the one taking a void* as second argument. This overload prints the value of the ... it, then instead of ... know how to generate a new ... Overloading new and delete in C++ - OOPortal - Object Oriented PortalThis page describes how to overload the operators new and delete in C++ overload - perldoc.perl.orgMost of the overloadable operators map one-to-one to ... during processing of use overload, no overload, new ... by the Overloaded function of module overload). The module ... 7/16/2012 12:18:43 PM
|