0

I'm attempting to port over some code to the new G++ on Ubuntu Server. I'm not sure what this code is trying to do but at quick glance, it's trying to delete the memory associated with a pointer.

    class mem_block
    {
     public:
      class mem_block*    next;
      void*               pntr;
      int                 size;

    mem_block( int i ) {
      record_new( sizeof( mem_block ), MEM_MEMORY );
      size  = i;
      pntr  = new char[size];
      return;
      }

    ~mem_block( ) {
      record_delete( sizeof( mem_block ), MEM_MEMORY );
      delete *pntr; // Troubled line.
      return;
      }
  };

Now I've tried delete [] pntr; (as it was originally), it's current form, etc... I'd hate to comment the code out as that would create a massive memory leak I'm sure.

Any ideas? It gives me unable to delete void* and in this case, void* is not a pointer-to-object type

Demortes
  • 161
  • 11

2 Answers2

3

You need to delete pointer itself not its referenced value, it should be:

delete [] (char*)pntr;
fatihk
  • 7,789
  • 1
  • 26
  • 48
  • Sorry, tried that. memory.h: In destructor ‘mem_block::~mem_block()’: memory.h:90:15: warning: deleting ‘void*’ is undefined [enabled by default] – Demortes Apr 30 '13 at 05:52
  • @Demortes as long as your void* points to a primitive type it is safe to delete as discussed in the link: http://stackoverflow.com/questions/941832/is-it-safe-to-delete-a-void-pointer – fatihk Apr 30 '13 at 05:57
  • @Demortes why do you use a `void*` in the first place? Why a pointer at all? – juanchopanza Apr 30 '13 at 06:08
  • 1
    @Demortes, you can also use char* explicitly to remove warning – fatihk Apr 30 '13 at 06:11
  • Tried the char* and it failed.memory.h: In destructor ‘mem_block::~mem_block()’: memory.h:90:12: error: expected primary-expression before ‘char’ memory.h:90:12: error: expected ‘;’ before ‘char’ – Demortes Apr 30 '13 at 06:14
  • @juanchopanza I didn't create the code original and not entirely sure how to work around it. It was originally pointer, so I'd like to keep it a pointer until I find out what uses it, etc. – Demortes Apr 30 '13 at 06:18
  • 1
    Like this `delete[] (char*)pntr;` – john Apr 30 '13 at 06:21
  • I should have known that @john. Thanks. – Demortes Apr 30 '13 at 06:26
  • @faith_k: I read that link and the consensus seems to be that it is **not** safe to delete without the cast as, without it, the `delete[]` causes UB. It's not so much that you _can_ use the cast to remove the warning, it's more that you _must_ cast the pointer to the correct type to avoid UB. – CB Bailey Apr 30 '13 at 06:52
2

From C++ Standard [Section 5.3.5(3)]: "In the second alternative (delete array) if the dynamic type of the object to be deleted differs from its static type, the behavior is undefined."

In your case, the dynamic type of pntr is char*, hence use C++ casting to explicitly inform the compiler of its type.

delete [] (static_cast<char*>(pntr));
Arun
  • 2,087
  • 2
  • 20
  • 33