overload operator new in one module

  • Follow


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:













7/16/2012 12:18:43 PM


Reply: