Green goblin
Green goblin

Reputation: 9994

Conversion of void* in C vs C++

In C, a void pointer is implicitly typecasted to another type.

See below program:

int main()
{
    void *p;
    int* ptr,i=5;
    p=&i;
    ptr=p; <---------------------------
    return 0;
}

The program compiles successfully when run under C environment.

However, if the same program is run under C++ environment, i am getting the below error:

prog.cpp: In function ‘int main()’:
prog.cpp:8: error: invalid conversion from ‘void*’ to ‘int*’

It means that in C++, we need to explicitly typecase a void pointer.

So, Why the return type of the new operator is void*? How, it is being converted to the desired type?

Upvotes: 4

Views: 555

Answers (7)

Mike Seymour
Mike Seymour

Reputation: 254751

So, Why the return type of the new operator is void*?

Do you mean the keyword new, used in a new-expression like int * p = new int[42];? That expression has the correct type, in this case int*, so no conversion is necessary.

Or do you mean the memory allocation function operator new()? Like malloc(), that is dealing with raw memory, not typed objects, and so its return type is an untyped void*. It's used internally by a new-expression, which knows the type of the object being created and so can perform the necessary pointer conversion once the objects have been fully constructed.

On the very rare occasions when you want to use it in your own code, you'll have to convert it to the correct type yourself; but you'd nearly always use it indirectly through a new-expression, and not have to worry about such details.

Upvotes: 0

tenfour
tenfour

Reputation: 36906

If you overload operator new, you indeed return a void*. An overloaded operator new is basically a simple allocation function like malloc() in C.

But when you use operator new, you are doing more than just calling that function; you also implicitly call the constructor for example. Returning the exact correct type is another implicit difference between constructing with new, and your overloaded operator new function.

C++ gives you an error because void* could point to anything. So if you try to assign it to something specific, you are making an assumption that may not be true. As C++ is strongly-typed, you must tell the compiler about your assumption and be explicit about it; that's the protection mechanism that type safety provides. C is not strongly typed and it's solely the responsibility of the developer to take care.

Upvotes: 3

BxlSofty
BxlSofty

Reputation: 501

You need to enter:

  ptr = (int *) p;

The result of new is not a void *. It depends on the object you are creating. If you do

  ptr = new int [2];

there's no recasting involved.

Upvotes: 1

Jim Balter
Jim Balter

Reputation: 16424

http://www.cplusplus.com/reference/std/new/operator%20new/

operator new can be called explicitly as a regular function, but in C++, new is an operator with a very specific behavior: An expression with the new operator, first calls function operator new with the size of its type specifier as first argument, and if this is successful, it then automatically initializes or constructs the object (if needed). Finally, the expression evaluates as a pointer to the appropriate type.

Note the final sentence.

Upvotes: -1

valdo
valdo

Reputation: 12951

C++ imposes more strict rules to pointer cast. One of the reasons is probably because pointer cast in C++ eventually may lead to a machine code, i.e. it's not a purely cosmetic thing.

For example class A inherits B and C (in this order). If you take a pointer to C and cast it to A - this result to a (raw) pointer shift.

Upvotes: 1

JeremyP
JeremyP

Reputation: 86691

Because new is not a function, it is an operator and is handled specially by the compiler.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409482

The new operator is treated specially by the compiler, it get the type from the argument to new and does an implicit cast to the correct type.

If you create your own operator new you have to return a void pointer, since you actually doesn't know the type, just the amount of memory you need to allocate.

Upvotes: 1

Related Questions