7

I have the following code;

void* buffer = operator new(100);
unsigned char* etherhead = buffer;

I'm getting the following error for that line when trying to compile;

error: invalid conversion from ‘void*’ to ‘unsigned char*’

Why do I get that error, I thought a void was "type-less" so it can point at anything, or anything can point to it?

SoapBox
  • 20,457
  • 3
  • 51
  • 87
jwbensley
  • 10,534
  • 19
  • 75
  • 93

6 Answers6

12

You need to cast as you can not convert a void* to anything without casting it first.

You would need to do

unsigned char* etherhead = (unsigned char*)buffer;

(although you could use a static_cast also)

To learn more about void pointers, take a look at 6.13 — Void pointers.


The "type-less" state of void* only exist in C, not C++ with stronger type-safety.

Michael Karcher
  • 3,803
  • 1
  • 14
  • 25
josephthomas
  • 3,256
  • 15
  • 20
5

A void* might point at anything and you can convert a pointer to anything else to a void* without a cast but you have to use a static_cast to do the reverse.

unsigned char* etherhead = static_cast<unsigned char*>(buffer);

If you want a dynamically allocated buffer of 100 unsigned char you are better off doing this and avoiding the cast.

unsigned char* p = new unsigned char[100];
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
4

You can convert any pointer to a void *, but you can't convert void * to anything else without a cast. It might help to imagine that "void" is the base class for EVERYTHING, and "int" and "char" and whatnot are all subclasses of "void."

mjfgates
  • 3,351
  • 1
  • 18
  • 15
3

Here's a bit of lateral thinking: Whenever you think you need casts or pointers, think again. If all you need is 100 unsigned bytes of memory, use

std::array<unsigned char, 100> data;

or

unsigned char data[100];

If the size is not constant, use a vector:

std::vector<unsigned char> data(size);

Raw pointers, the new operator, and casts are unsafe, hard to get right and make your program harder to understand. Avoid them if possible.

Philipp
  • 48,066
  • 12
  • 84
  • 109
1

C++ is designed to be more type safe than C. If this is C code, it may be OK, but also depends on what compiler you are using right now.

Also, technically, "extern "C" int *" and "int *" are different types... (like solaris compiler will pick this out)

I would suggest you using C++ style cast instead of C cast. There are more descriptions here:

Regular cast vs. static_cast vs. dynamic_cast.

Community
  • 1
  • 1
Spike
  • 302
  • 2
  • 4
0
void *pt; 

pt=(void*)&i; 
pt=(void*)&dbl; 

Here's how I would do it.

Erwald
  • 2,118
  • 2
  • 14
  • 20
  • Oh :( I thought you could, I though you could allocate X bytes in memory for later use, because a 10 byte char or a 10 byte int are both 10 bytes long, irrelevant of the values of the alloted 10 bytes. Is this wrong? – jwbensley Apr 14 '12 at 20:55
  • Is there a compelling reason you want to re-use the memory? If not, then don't do it, it makes the program unnecessarily complex. – Philipp Apr 14 '12 at 21:56