12

The definition of new in the <new> header is:

 void* operator new(size_t);

And the definition of malloc is as stated:

 void* malloc(size_t);

Now, as C++ is a strongly typed language, it requires a cast from the programmer to convert a void* pointer to the type the programmer requires... In malloc, we have to perform a cast, but not in new, though both return a void* pointer. Why?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bhuwansahni
  • 1,834
  • 1
  • 14
  • 20

3 Answers3

13

Because when you're using new, you (normally) use a "new expression", which allocates and initializes an object. You're then assigning the address of that object to a pointer to an object of the same (or parent) type, which doesn't require a cast. A normal new expression (i.e., not a placement new) will invoke operator new internally but the result of the new expression is not just the result from operator new.

If you invoke operator new directly, then you need to cast its result to assign the return value to a non-void pointer, just like you have to do with the return from malloc.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
5

Because you have the wrong impression how to use malloc. It doesn't receive the type as an argument but only the size of the type.

C and C++ are different languages. In C you don't need to cast the void* of malloc to the target pointer type. In C++ the new operator is deeply build into the language such that it always returns a value corresponding to the type you gave in the argument.

The rule is quite simple use new for C++ and malloc for C, don't mix them.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • How does new receives the type as the argument?? – bhuwansahni Mar 07 '12 at 06:53
  • 1
    @bhuwansahni, `new` is an operator not a function. In the place that you use it you always give it the type of the object that is to be allocated and constructed. It is up to the compiler to make the link. In C for a call to `malloc` you only give the size. – Jens Gustedt Mar 07 '12 at 06:57
  • 1
    @bhuwansahni, because it is not necessary. To write a generic `operator new` the only property of the target type you need is the size. The type specific stuff is ensure by compiler by calling the appropriate constructor for the type. – Jens Gustedt Mar 07 '12 at 07:14
  • 1
    @bhuwansahni: you are confusing the new operator and operator new. They are two different things. The new operator is a syntax for allocating and constructing objects. Operator new is a special declaration syntax for declaring and writing functions which implement memory allocation, connected to the new operator. new int(3) knows the size of int in the same way that sizeof(int) does: they are operators that operate with types. The new operator calculates the size and passes it to the appropriate operator new function. – Kaz Mar 07 '12 at 08:39
2

Because the operator new call is only a single step generated in the entire chain when you invoke new.

When you do s=new my_type(args);, the compiler will expand that to:

s = (my_type*)my_type.operator new(sizeof(my_type));
s.my_type(args); //Constructor call
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ratchet freak
  • 47,288
  • 5
  • 68
  • 106