-4

whats wrong with the code. What it should be. As it is throwing an error. Operator 'delete', applied to void* argument.

int i;
void *ptr = &i;

delete ptr;
Anand
  • 67
  • 4
  • 9

4 Answers4

7

whats wrong with the code.

Everything except int i;

The second line attempts to convert an integer to a pointer. In special circumstances, you could force that past the compiler with reinterpret_cast; but the program would only behave correctly if the integer somehow contained a valid pointer value. (UPDATE: the question has now been edited to take the address of i instead; so this line is no longer wrong).

The third line attempts to delete invalid memory using an invalid pointer type.

What it should be.

Something else. Without knowing what you want to do, it's impossible to say. Perhaps you want:

int i;
void * ptr = &i;  // Points to `i`, losing type information.

Operator delete, applied to void* argument.

That's always wrong. You must only delete an object you previously allocated with new; and the pointer must be to the correct type.

(or to a base class, if it's a class type with a virtual destructor)

So the following would be correct; but pointless unless you have a good reason for dynamic allocation:

int * ptr = new int;
delete ptr;

And of course, if you're writing code that should be robust against memory leaks and runtime errors, you should manage all dynamic memory using RAII types such as containers and smart pointers. So, if you need to a dynamic object, you should do:

std::unique_ptr<int> ptr(new int);

if you want a to delete it at the end of the current scope or move it into another scope; or

auto ptr = std::make_shared<int>();

if you want to share ownership between more than one scope. In either case, the pointers will delete the object automatically once you've finished with it.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
2

You should only delete things that you allocated with new.

QI3it
  • 181
  • 7
  • Absolutely true, but that is not the reason for the error message. – Gorpik May 29 '13 at 10:31
  • Yes, since you cannot allocate a pointer of type void with new you also cannot 'delete' it. – QI3it May 29 '13 at 10:37
  • You can. Just change that code to `int* i = new int; void* ptr = i;`. `ptr` now points to an object created with `new`, but you cannot delete it because of its type. – Gorpik May 29 '13 at 10:41
  • But you didn't *allocate a pointer of type void*, you allocated one of the type int and assigned it to a void pointer what is actually possible. – QI3it May 29 '13 at 10:46
  • If `Derived` inherits from `Base` and they have virtual destructors, `Derived* i = new Derived; Base* ptr = i; delete ptr;` is perfectly legal. You cannot delete a `void*` because `void` is not a type, that's all. – Gorpik May 29 '13 at 10:51
  • Yes, you are right. The only thing I wanted to say is that it is also not legal to use new like so: `void *ptr = new void`. But to the question above your answer is absolutely correct. Sorry if I confused anyone by throwing in memory allocation although it was a syntax error. – QI3it May 29 '13 at 11:02
1

Very naughty code indeed;

  1. You're not taking the address of i; use void* ptr = &i instead. You're compiler is extremely lenient to emit your code.
  2. You're deleting from the stack. Not good. You can only delete things you've newed
  3. The compiler doesn't know how much memory to delete if you pass void* as all the sizeof data are lost.
Juraj Blaho
  • 13,301
  • 7
  • 50
  • 96
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

your code will not work because you're trying to delete something which wasn't dynamically allocated.
Anyway deleting void pointers is undefined, see this Q&A for more info

Community
  • 1
  • 1
spiritwolfform
  • 2,263
  • 15
  • 16
  • the link you have mentioned does not answer my question. int *i = new int; void *ptr = i; delete ptr; Is this ok ? – Anand May 29 '13 at 10:21
  • @Anand It answers your question pretty well. Look at the second answer. – Gorpik May 29 '13 at 10:36