473,545 Members | 721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determining if object has been allocated using operator new

Hello,

I want to make my garbage collector more safe. To make it more safe I
need to know if an object has been allocated on the stack or on the
heap using the operator new. My garbage collector uses a mixture of
reference counting and smart pointers. I have got a base class
("Object") which handles all the memory management stuff.

I have worked out different approaches to pass the info to my base
class (won't name all):
- Cast the allocated memory chunk to the base object and set directly
the heap-flag. This will most likely fail with virtual and multiple
inheritance. A template operator new (not placement new) would be
great.

- Set upon call of the operator new a static bool flag if there is
currently a not yet constructed (but already allocated) object in the
heap. The Object constructer will then determine wether or not the
object is heap-allocated. This might fail if I have multiple base
classes - the construction order might be wrong, and if other objects
are constructed before, it might happen that other objects will be
marked incorrect.

struct Object
{
Object()
{
if (heapObject)
isHeapAllocated = true; //If another object has been constructed
before, "isHeapAllocate d" will be false, even though it should be true.
else
isHeapAllocated = false;
heapObject = false;
/* Do some MM */
}

void* operator new(size_t objectsize)
{
void *newObject = malloc(objectsi ze);
heapObject = true;
return newObject;
}

/* Many other member functions */

bool isHeapAllocated ;
static bool heapObject;
};

- Add the allocated object to a list. When the Object constructor is
called it will search the list for a value equal to "this".

struct Object
{
Object()
{
/* Might not find anything. */
std::list< Object* >::iterator it = std::find(heapO bjects.begin(),
heapObjects.end (), this);

if (it != heapObjects.end ())
isHeapAllocated = true;
else
isHeapAllocated = false;
}

void* operator new(size_t objectsize)
{
void *newObject = malloc(objectsi ze);
heapObjects.pus h_front(static_ cast< Object* >(newObject)) ;
return newObject;
}

/* Many other member functions */

bool isHeapAllocated ;

static std::list< Object* > heapObjects;
};

The search will fail if the Object base class has not the same address
like the full object - which might be the case with virtual or multiple
inheritance.

- The last possibility would be to wrap the new operator, and instead
use own functions or factories for allocation and construction. I would
need to rewrite a lot of code. And I bet many new issues would arise.
Have you got any ideas? Maybe I can somehow rethink my memory
management.

Thanks in advance,
Robert

Aug 31 '05 #1
0 1640

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

Similar topics

106
5479
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the same as an assignment, but for class types it has a different effect - it calls the copy constructor. My question is when to not use an...
7
1466
by: Dave | last post by:
Hello all, In the code below, I use a pointer to an object under construction. Is the usage below legal? I have come across similar code at work. It compiles, but I'm not sure it's really legal... Thanks, Dave struct D;
30
3696
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g = 111; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl;
7
2845
by: Arpan | last post by:
The .NET Framework 2.0 documentation states that An Object variable always holds a pointer to the data, never the data itself. Now w.r.t. the following ASP.NET code snippet, can someone please explain me what does the above statement mean? <script runat="server"> Class Clock
6
2239
by: Pablo | last post by:
Hello, I am writing a windows application using C++ and BorlandBuilder 6 compiler. It is an event driven program and I need to create objects of some classes written by me. One of the classes contains a pointer to int values as a filed. In the definition (implementation) of constructor I use this pointer to create table of int values with the...
1
1999
by: Martin | last post by:
Hi. I've found a few topics on this subject, but still not sure and decided to post mine. Here's my example: #include <iostream> using namespace std;
4
3695
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and value-initialization. I think default-init calls default constructor for class objects and sets garbage values to PODs. Value-init also calls default constructor for...
14
1436
by: Summercool | last post by:
The meaning of a = b in object oriented languages. ==================================================== I just want to confirm that in OOP, if a is an object, then b = a is only copying the reference. (to make it to the most basic form: a is 4 bytes, let's say, at memory location 0x10000000 to 0x10000003
7
1329
by: rn5a | last post by:
Consider the following code: ------------------------------------------- Class MyInt Public MyValue As Integer End Class Sub Page_Load(..........) Dim x As New MyInt Dim y As New MyInt
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
5969
agi2029
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...
1
5325
isladogs
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...
0
4944
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...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1878
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
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.