473,723 Members | 2,204 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

operator new, delete, static variable lifecyle and more


For several reasons I want to replace the built-in memory management with
some custom built. The mem management itlsef is not subject to my question -
it's ok to the point that I have nice and working allocation deallocation
routines. However, I don't want to loose the nice extras of new operator,
like - constructor calling, typecasting the result, keeping the array size,
For another bunch of reasons, outside this scope I want the memory
management to be global, and not per-class.
So it seemed pretty obvious to me that I could overload the global new
operator and do the work. And here start the series of problems with
mutually exclusive solutions:

- When I try to use STL they all refer to my new and delete operators. This
by itself is not bad, BUT. My memory manager (so to say) is static variable
(singleton based implemetantion) and so it has some lifecyle. Unfortunetely
some static variable in STL - like 'locale'-s managent have longer life and
at some points of program finish it refers to some memory that my manager
has already freed. Since I cannot make my object to live longer than STL's -
this leaded to different approach:
- I tried to overload the new operator in an unique way - i.e. with some
useless parameter to prevent STL from using it. so far so good. But
overloading delete operator in such a manner is not possible, since it does
not accept additional parameters. So I came up with solution in which for
allocation I use my custom made new operator and for deallocation - a
function call. With macroses it could look nice. But here comes the next
- Except deallocation delete operator calls destructors. This, by itself is
not a problem, BUT. For example on VC7 compiler the new[] operator allocated
sizeof(size_t) bytes more at the beginning to put the array size there and
__returns the pointer AFTER those sizeof(size_t) bytes__. Even that is not
SO bad. But when we add two more facts - (1) when the type allocated has no
destructor the new operator does NOT allocate that extra storage, (2) none
of the above stays in the C++ standard. So implementing delete[] on you own
involves knowledge whether passed type has destructor or not. And again -
this is pretty compiler specific solution if there is any at all.

Please, if anybody sees some mistake or misjudgement in my observation or
has an idea how I can workaround this and make it possible to use new and
delete without rewriting tons of automatic code - give my an advice.
Thank you in advance,
Jul 22 '05 #1
0 2861

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

Similar topics

by: Senthilvel Samatharman | last post by:
I am just curious about the case 3 in the follwouing program. I understand that case 1 is the right way to overload, while Case 2 is erroneous. But i do think that the implementation of operator delete of Case 3 should end up in recursion but works fine. Am i wrong or is it the problem with the VC6 compiler? thanks , Senthil.
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have nice and working allocation deallocation routines. However, I don't want to loose the nice extras of new operator, like - constructor calling, typecasting the result, keeping the array size, etc. For another bunch of reasons, outside this scope I...
by: Amy | last post by:
Hello, We are developing C++ appplications for PDAs where memory is limited, so we want to do memory management by ourselves --- pre-allocated a big chunk and overwrite new and delete to call our APIs. The tricky thing simply redefine operator new and delete because the OS(PDA platform OS)library we need to link with in our application also redefined operator new/delete for some purpose. Simply redefine new/delete as following won't...
by: ct | last post by:
Hi, Here is my problem: I need to be able to call a virtual method when an object is deleted. class A { public: virtual ~A(); void virtual DestroyRespond();
by: lothar.behrens | last post by:
Hi, I have problems to delare a delete operator in a class and use it to check for valid pointer. Using release() with an additional validation routine from a separate malloc library avoids the double deletion. The first delete will call the destructor and after it the delete operator
by: tom | last post by:
Hi, I'm overriding my operator new and operator delete for two classes, one inherited from the other, so I can use my own memory pool. A simplified version of what I have is below: class BaseClass { BaseClass(); virtual ~BaseClass(); virtual void baseFunction();
by: silversurfer2025 | last post by:
Hello everyone, once again, I have a very basic problem in C++, which I was not able to solve (maybe because of my Java-Experience or just because it is always the small syntax-things which break my neck in C++)... I would like to have a static variable in class A and change its value from class B (they are not related to each other) For example:
by: Lighter | last post by:
Big Problem! How to overload operator delete? According to C++ standard, "A deallocation function can have more than one parameter."(see; however, I don't know how to use an overloaded delete operator. Let me use an example to illustrate this: /********************************************************/ #include <new> #include <iostream>
by: Nindi | last post by:
On comp.lang.c++.moderated http://groups.google.co.uk/group/comp.lang.c++.moderated/browse_thread/thread/8250715711da7760?hl=en the following question was posted ------------------------------------------------------------------------------------------------------- Is this standard conformant to type cast ::opearator delete to a function of type (void (*)(void*)); Is not operator delete already a function of this type?
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods are the one which runs statically with the defining class. Hence, my understanding is that static variables must be bound to the class defining the variables and shared by children of parent class where the variable is defined. But, please have a...
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.