Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all :
i writing a double list and i want to write RemoveAll function for this.in CNode class :
C++
class CNode
{
     private :
        CNode* pNext;
        CNode* pPrev;
        void*  pObj;
     public :
        ...
        ~CNode(void)
         {
             delete pObj;
         }
}

but compiler gives a warning ""can not delete expression with pointer to-void-type "void *".
i create pObj with new operator;
it is very important for me that memory release correctly(i working with microcontrollers and it have only 32 KB ram).
please explain me how i can remove a object that void pointer point to it(i do not know what pObj type and can not use type casting operator).
Posted
Updated 26-Jul-13 2:52am
v2
Comments
pasztorpisti 26-Jul-13 9:45am    
void* is used quite rarely in C++: to address untyped binary data and may in case of some low level libraries that do magic (for example serialization). void* has no right to be present in your C++ code most of the other cases. Containers in C++ should be implemented as templates:

template <typename NodeDataType>
class TNode
{
TNode* pNext;
TNode* pPrev;
NodeDataType* pObj;
...


In my opinion using hungarian notation is totally unnecessary (just adds noise to the source code) with todays IDEs and refactoring tools. In C++ you don't have to specify (void) as function parameter list. In C (void) was needed because an empty list is vararg by default in C but the same isn't true for C++. In C++ an empty parameter list isn't vararg and (void) is only backward compatibility with C.

1 solution

Of course. How much of memory should be deleted? With untyped (void) pointer, a compiler could not find it out.

One of the valid approaches with C++ is this. Node object obj can be of some abstract base type, with virtual destructor. All the runtime types of the actual obj objects will be derived from this base type, overriding the destructor, which would be a mechanism to implement the release for this object, no matter how complex it is.

One of the simplest mechanisms would be to have an array of some, say, fixed type. Arrays can be deleted based on the current length known to the memory allocator via the operator delete[]:
http://www.cplusplus.com/reference/new/operator%20delete%5B%5D/[^].

—SA
 
Share this answer
 
Comments
nv3 26-Jul-13 9:31am    
+5 and how true. What I sometimes wonder is: Do beginners read any manuals these days? :-)
Sergey Alexandrovich Kryukov 26-Jul-13 9:42am    
Thank you.
As to OP: at least this one was able to provide relevant and sufficient information on the problem. I wish others would be able to ask a question like that. For a beginner, the question is good enough.
—SA
pasztorpisti 26-Jul-13 9:39am    
5. BTW, the problem (writing a container) seems to me like something that is typically solved with templates in c++.
Sergey Alexandrovich Kryukov 29-Jul-13 18:18pm    
Thank you.
Templates are of course important, but this is nothing but an abstraction used to write code agnostic to some type(s). In my suggestion, I describe the very different, non-static approach to data type, based on polymorphism. It all depends on different kinds of requirements.
—SA
pasztorpisti 29-Jul-13 18:20pm    
You are right. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900