Question about C++ delete operator

jgbishop

Senior member
May 29, 2003
521
0
0
Suppose that I create a variable using the new operator, and then assign the resulting pointer to a void* variable. Much later in the program (perhaps when the user exits), I obtain the void* and free the memory with the delete operator.

If I delete the void*, how does the delete operator know how much memory to free (since void* can point to any type)? Is that information stored away so the computer knows what to do?

Here is an example:

myType* foo = new myType();
m_voidp = foo; // m_voidp is a member variable of type void*

... Much later in the code (not even in the same function)

delete m_voidp;
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Not a good idea. You should only delete a pointer of the same/related type as the type of the allocated object. I believe the result is undefined otherwise.
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
If my memory serves me correctly the application maintains that information. For example when you do "delete [] pSomePointer" the delete operation knows how many elements there were in the array and frees all of them up.
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
Originally posted by: dighn
Not a good idea. You should only delete a pointer of the same type as the type of the allocated object. I believe the result is undefined otherwise.

That is incorrect. You can always do:

ParentClass* pTemp = new ChildClass();


Even though pTemp points to the wrong data type the deletion would work correctly.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: Argo
Originally posted by: dighn
Not a good idea. You should only delete a pointer of the same type as the type of the allocated object. I believe the result is undefined otherwise.

That is incorrect. You can always do:

ParentClass* pTemp = new ChildClass();


Even though pTemp points to the wrong data type the deletion would work correctly.

But the parent type is still part of the child type, void* is different though. I just tried it in VC++ 7. If I delete an object as void*, the destructor is never called, very bad.

Good point though, edited my post to be clearer.
 

jgbishop

Senior member
May 29, 2003
521
0
0
So how can I safely remove data that is of type void*? Should I cast it to the appropriate type and do the delete that way?

My situation is this: I have a container class that wraps a number of structures (all generated by other programmers). In order to accomodate all of these types, I store the data as a void* and do the appropriate cast in the appropriate places. I'll need to clean up this data, so what's the proper way to do it?
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
The correct implementation would be for the user of your class to clean up the objects. If you look at all standard c++ container libraries none of them do the clean up. One of the big reasons for that is you don't know who else is using that pointer.
 

jgbishop

Senior member
May 29, 2003
521
0
0
Actually, I should correct myself. Here is my actual situation (I just looked back at my code to verify what I was doing - sorry for the confusion).

I have a linked list class that contains a number of Nodes (objects from a Node class). The Node class contains one data member, which is of type void*. If I instantiate a new Node, then delete it, are things taken care of properly?

Example:

Node* n = new Node(data); // data is a void*

... later on

delete n;
 

akubi

Diamond Member
Apr 19, 2005
4,392
1
0
Originally posted by: jgbishop
Actually, I should correct myself. Here is my actual situation (I just looked back at my code to verify what I was doing - sorry for the confusion).

I have a linked list class that contains a number of Nodes (objects from a Node class). The Node class contains one data member, which is of type void*. If I instantiate a new Node, then delete it, are things taken care of properly?

Example:

Node* n = new Node(data); // data is a void*

... later on

delete n;

huh? you have a void* member in Node class? are you dynamically allocating memory with that?

then you need to manually write Node's destructor to clean that part up.

 

jgbishop

Senior member
May 29, 2003
521
0
0
The void* is actually never allocated in the Node class. Let me attempt to better explain what's going on (I'm a little tired, and I'm not thinking too straight).

I have a number of structures which I am populating with data. These structures are created by another library I am using, and I get the chance to access them through a callback method. After the callback method is finished, the structures are deleted by the library. In order to keep these structures around (so that I can make use of them later on in program execution), I make a deep copy of them. So, I create a new structure using the new operator, perform a deep copy (by simply doing a dereferenced pointer assignment), and assign the resulting pointer to the void* data member in a node in my linked list.

When I go to delete the linked list, I'm concerned that the void* memory will be leaked (since it was created with the new operator). I'm guessing that I will need to do the appropriate cast and delete in the Node destructor. Each node knows what data type it holds, making my life much easier in that regard.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Is it possible to create an abstract base class (ABC) that these various datatypes would all inherit from?
Then you could have an ABC pointer in the node instead of the void pointer, and inheritance takes care of calling the right destructor when you leave.
 

jgbishop

Senior member
May 29, 2003
521
0
0
Originally posted by: Armitage
Is it possible to create an abstract base class (ABC) that these various datatypes would all inherit from?
Then you could have an ABC pointer in the node instead of the void pointer, and inheritance takes care of calling the right destructor when you leave.

I've thought of doing that, but the problem is that I have quite a number of these structures to handle (40 or so). Maintaining that number of classes would become quite a chore, so I'm trying my best to keep the amount of code to a minimum.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: jgbishop
Originally posted by: Armitage
Is it possible to create an abstract base class (ABC) that these various datatypes would all inherit from?
Then you could have an ABC pointer in the node instead of the void pointer, and inheritance takes care of calling the right destructor when you leave.

I've thought of doing that, but the problem is that I have quite a number of these structures to handle (40 or so). Maintaining that number of classes would become quite a chore, so I'm trying my best to keep the amount of code to a minimum.

The ABC could be extremely minimal - something like this:

class ABC
{
public:
virtual ~ABC(void) = 0;
};

Then add the following to all of your other classes:

class MyClass : public ABC

There may be more to it then that, I haven't tried it.
 

jgbishop

Senior member
May 29, 2003
521
0
0
Originally posted by: Armitage
The ABC could be extremely minimal - something like this:

class ABC
{
public:
virtual ~ABC(void) = 0;
};

Then add the following to all of your other classes:

class MyClass : public ABC

There may be more to it then that, I haven't tried it.

Yes, that is exactly what it would look like. But, like I say, I would rather not have a single class for each structure (I currently do not).
 

bersl2

Golden Member
Aug 2, 2004
1,617
0
0
Originally posted by: BingBongWongFooey
Templates sound like what you want.

Yeah.

void * is a C thing.
delete is a C++ thing.
Mixing the two might not be a very good idea.