void pointers (be deleted ?)

Can void pointers be deleted if they have been created as new objects?

I had been confused with void pointers when I heard that they can't be reassigned once they have been assigned.
closed account (zb0S216C)
I do believe that resources allocated by a void * object can release the resources it allocates. However, I think that the contents of the resource block cannot be modified since the type is truly unknown.

You will need to explain further as I'm not 100% sure what you're asking.

Wazzak
Do you mean something like this?

1
2
void** p = new(void*); // Create a void pointer using new
delete p; // delete it 
I wanted to do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
	void ** vpptr_ = new void*;
	int iP;
	char cP;
	//assigning void pointer
	*vpptr_ = &iP;
	delete []*vpptr_; //clearing container

	//reassigning void pointer
	*vpptr_ = &cP;
	delete []*vpptr_; //clearing container


	return 0;
}


The problem is that I'm getting an assertion error
yes @Moschops
delete []*vpptr_;

That just doesn't make any sense; use the delete[] notation for deleting something that was created using new type[x].

What do you mean by "clearing container"? delete doesn't clear anything - it marks memory as available for reuse. It is an error to delete an object that wasn't allocated using new.
Last edited on
I thought "Clearing the pointer would allow me to reuse it". But thanks it now works and the revised code is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	void ** vpptr_ = new(void*); 
	int iP;
	char cP;
	//assigning void pointer
	*vpptr_ = &iP;
	delete []vpptr_; //clearing container

	vpptr_  = new(void*); 
	//reassigning void pointer
	*vpptr_ = &cP;
	delete []vpptr_; //clearing container


	return 0;
}
Your code is still incorrect. You are still using "delete[]" instead of "delete", and now you are putting the new type in () which is used as parameters for overloads of new like placement new.
the old system,what you've just suggested ("delete") alone, was causing an assertion error
The code I suggested in answer to your original question (create void pointer with new and then delete it) compiles and runs without error (although if you're so hard-pressed for memory that you need to make your pointers on the heap, you've got bigger problems).

1
2
3
4
5
6
int main()
{
void** p = new(void*); // Create a void pointer using new
delete p; // delete it 
return 0;
}


Your code is an attempt to use delete on an object that was not allocated using new. When you allocate something using new, you get back a pointer to that object. If you then take that pointer and make it point to something else completely, as in your code, that doesn't magically mean you can use delete on what it now points to.

You code, as it stands now, applies delete to objects that were not allocated using new. This is wrong. Do not be fooled by the fact that your code compiles. The compiler has no option but to trust that you know what you are doing.

What exactly are you trying to do? I'm not convinced that you understand what delete is for.
Last edited on
I was just trying to use a void pointer that could be pointed to any datatype and then changed to point to another. And fortunately the system I now have is the one that works since it doesn't cause an assertion error as before. It might seem confusing but try executing both of them
Oh right. Yes, that can be done. You don't need to use new.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

 int main()
{
  void* crazyPointer;
  int a = 7;
  char b = 'x';
  std::string c("Beans on toast");


  crazyPointer = &a; // Now it points to the int
  std::cout << *(int*)crazyPointer << std::endl;
  crazyPointer = &b; // Now it points to the char
  std::cout << *(char*)crazyPointer << std::endl;
  crazyPointer = &c; // Now it points to the std::string
  std::cout << *(std::string*)crazyPointer << std::endl;

  return 0;
}


If you use a void* to dereference something, remember to cast it to the right type. The casting above is C style casting which is very lazy, but if you're casting void pointers you've already thrown away all the advantages of type-checking anyway.

It might seem confusing but try executing both of them


Your code compiles because the compiler has to trust you. It runs because it doesn't actually cause a runtime error. It is, nonetheless, wrong. I don't know how else to say this. Just because it compiles does not mean it's correct. Your code creates something on the heap when there is no need to (that's what you do with new) and then you go around using delete[] on things that it is wrong to use delete on.
Last edited on
Topic archived. No new replies allowed.